2

我正在使用 node-notifier 包进行通知,但我有一个问题。在 Windows 操作中心中未触发 notifier.on 单击事件

notifier.notify(
      {
        appId: "com.electron.demo", // Absolute path (doesn't work on balloons),
        title: "Alert Message",
        message: "Click to view",
        icon: path.join(__dirname, 'msicon.png'),
        wait: true,
        timeout: false,
        id: "demo app"
      },
      function(err, response) {
        // Response is response from notification
        if (err) {
          reject(err);
        } else {
          resolve(response);
        }
      }
    );

notifier.on("click",  function(notifierObject, options, event) {

      // Triggers if `wait: true` and user clicks notification
      var request = new sql.Request();
      let username = userName;
          request.query("update alerts.dbo.groupmessages set isRead = 1\
                            where pk = '"+ recordset.recordset[i].pk + "'\
                            and userFK = (select pk from alerts.dbo.users where username = '"+ username + "')\
                            and groupFK = (select groupFK from alerts.dbo.users where username = '"+ username + "')", function (err, recordset) {
            if (err) throw err
            console.log(err, recordset, 'User shit');
          });          

          function createWindow() {
            // Create the browser window.
            win = new BrowserWindow({
              width: 400,
              height: 400,
              frame: false,
              webPreferences: {
                nodeIntegration: true
              }
            });
            // and load the index.html of the app.
            ejse.data('message', recordset.recordset[i].message);
            win.loadFile('index.ejs')
          }
          createWindow();

        });
    });
  });

notifier.on 点击​​事件不会仅在 Windows 操作中心触发。请让我知道原因和解决方案。谢谢。

4

2 回答 2

1

Action Center 需要在本机代码中单独实现,而 node-notifier 没有。
您可以尝试使用node-powertoast并使用 onActivated 回调: npm i node-powertoast

const toast = require('powertoast');

toast({
  title: "Hello",
  message: "world",
  callback: { 
    timeout: 5000, //keep-a-live in ms
    onActivated: ()=>{ console.log("activated") },
    onDismissed: (reason)=>{ console.log(reason) }
})
.then(()=> console.log("Notified")
.catch(err => console.error(err));

至于原因:
对于其他项目,您可以看到这是一项重大任务,例如,以下项目似乎让 Action Center 支持陷入困境 2 年了:https ://github.com/mohabouje/WinToast/issues /35试图自己实现它的人似乎陷入了困境,并且难以正确实现它。
这很难做到。

于 2020-06-06T21:15:01.847 回答
0

通知没有获得点击,因为并非所有操作中心要求都得到满足。

通常,Windows 通知具有三种激活类型foregroundbackgroundprotocol

在任何情况下,您都需要具备以下条件:

  1. 在开始菜单中有一个带有AppUserModelId属性的快捷方式。请咨询您的安装程序,了解如何操作或使用shell.readShortcutLinkElectron shell.writeShortcutLinkAPI。具体来说,WiX 允许您在选项中指定appUserModelId和。toastActivatorClsid

  2. AppUserModelId在应用程序中指定。

     app.setAppUserModelId('AppUserModelId');
    
  3. 注册一个自定义协议并对其做出一些反应。例如像这样:

     app.setAsDefaultProtocolClient('example');
    

要使用foregroundbackground您需要:

  1. 在开始菜单中有一个带有ToastActivatorCLSID属性的快捷方式。请参阅上面的操作方法。

  2. 使用像electron-windows-interactive-notifications 之类的项目来注册一个具有前面指定的 COM 服务器ToastActivatorCLSID和您选择的自定义协议并调用:

     registerComServer();
     registerActivator();
    

我不会深入了解如何对自定义协议做出反应,但您需要解析命令行参数以进行搜索,example:并且second-instance如果应用程序已经启动,还需要在钩子中使用它。

使用protocol类型不需要额外的操作。

之后,即使是标准的 Electron 通知也会从 Action Center 获得点击。

但是如何使用这些协议并获得功能丰富的通知呢?Electron 允许您直接指定 toastXml!

假设这是您的通知。https://google.com即使从操作中心单击它也会打开。

const notification = new Notification({
    toastXml: `
<toast launch="https://google.com" activationType="protocol">
  <visual>
    <binding template="ToastGeneric">
      <text>Wonderman meets Superwoman</text>
      <text>In the eve of the new millennium, Wonderman challenges Superwoman to a bliniking contest.</text>
      <text placement="attribution">Mars Press</text>
    </binding>
  </visual>
</toast>
    `,
});
notification.show();

此通知只会打开 Google。您可以在Notification Visualizer App获得更多想法

于 2022-01-29T12:45:47.450 回答