如何监听 Windows 通知的点击事件?

How can I listen click event on Windows notifications?

我只是使用 node-notifier 包发送通知。此外,当我单击通知时,它必须转到 link。但是我听不到点击事件。包提供的事件什么都不做。这是我的代码:

const notifier = require("node-notifier");
const open = require("open");

notifier.notify({
  title: "Whosebug",
  message: "A message",
  wait: true,
  open: "https://whosebug.com/",
});

notifier.on("click", function (notifierObject, options, event) {
  open("https://sindresorhus.com");
});

这是我的通知:

我可以使用任何其他包。我只想听点击事件。

@user120242 的答案有效,但在通知消失后点击无效。有什么办法吗?我加了一个gif.

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

const toast = require('powertoast');

toast({
  message: "Google It",
  onClick: "https://www.google.com"
}).catch(err => console.error(err));

也支持回调函数onActivate。查看 link 中的文档以了解更多详细信息。


如何修复节点通知程序点击事件:

https://github.com/mikaelbr/node-notifier/issues/291#issuecomment-555741924
从版本 5

开始,点击不触发影响了很多人

由于动作名称的变化使用不一致
可以回滚到5.4.3,或者在线程中使用回调代替的建议。

npm uninstall node-notifier
npm i node-notifier@5

或者:

notifier.notify({
 ... options
}, (err, action, metadata) => { if(action==='activate') { open('https://whosebug.com') })

如果您有信心并且宁愿自己修复库,还有另一种可能性: https://github.com/mikaelbr/node-notifier/blob/v5.4.3/notifiers/toaster.js#L63
https://github.com/mikaelbr/node-notifier/blob/v5.4.3/lib/utils.js#L245
修补那些以解决 'activate' 并发出 'click' (在 master 分支中 "mapper" 函数不再存在)。