首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在notifier.on操作中心中未激发Windows单击事件

在notifier.on操作中心中未激发Windows单击事件
EN

Stack Overflow用户
提问于 2020-04-14 20:11:22
回答 2查看 796关注 0票数 2

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

代码语言:javascript
复制
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 Action Center中触发。请告诉我原因和解决方案。谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-06-07 05:15:02

Action Center需要在本机代码中单独实现,这是node-notifier所没有的。

您可以尝试使用node-powertoast,并使用onActivated回调:npm i node-powertoast

代码语言:javascript
复制
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));

至于原因:

你可以看到,对于其他项目来说,这是一项重要的任务,例如,下面的项目似乎让行动中心的支持停滞了两年:试图自己实现它的https://github.com/mohabouje/WinToast/issues/35人员似乎被卡住了,并且很难正确地实现它。

这是很难做到的。

票数 0
EN

Stack Overflow用户

发布于 2022-01-29 12:45:47

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

一般来说,Windows通知有三种类型的激活:foregroundbackgroundprotocol

在任何情况下,您都需要具备以下各项:

  1. 在“开始”菜单中有一个带有AppUserModelId属性的快捷方式。请咨询您的安装程序如何做到这一点,或使用shell.readShortcutLinkshell.writeShortcutLink电子API。具体地说,WiX允许在选项中指定appUserModelIdtoastActivatorClsid

  1. 在应用程序中指定AppUserModelId

app.setAppUserModelId('AppUserModelId');

  1. 注册一个自定义协议并对其进行一些响应。例如:

app.setAsDefaultProtocolClient('example');

要使用foregroundbackground,您需要:

  1. 在“开始”菜单中有一个带有ToastActivatorCLSID属性的快捷方式。有关如何操作的信息,请参阅上面的内容。

  1. 使用像electron-windows-interactive-notifications这样的项目向指定的早期ToastActivatorCLSID和您选择的自定义协议注册COM服务器,并调用:

registerActivator();;registerComServer()

我不会深入讨论如何响应自定义协议的细节,但在搜索example:时,您需要解析命令行参数,如果应用程序已经启动,还需要在second-instance钩子中解析它。

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

在那之后,即使是标准的电子通知也会从行动中心获得点击。

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

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

代码语言:javascript
复制
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上获得更多的想法

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61207372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档