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

dgk*_*nca 12 javascript node.js electron node-notifier

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

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

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

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

这是我的通知:

在此处输入图片说明

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

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

use*_*242 7

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

const toast = require('powertoast');

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

还支持 onActivate 回调函数。查看链接中的文档以获取更多详细信息。


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

https://github.com/mikaelbr/node-notifier/issues/291#issuecomment-555741924
点击不触发从版本 5 开始影响许多人

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

npm uninstall node-notifier
npm i node-notifier@5
Run Code Online (Sandbox Code Playgroud)

或者:

notifier.notify({
 ... options
}, (err, action, metadata) => { if(action==='activate') { open('https://stackoverflow.com') })
Run Code Online (Sandbox Code Playgroud)

如果您有信心并且宁愿修复库本身,还有另一种可能性: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
修补它们以说明“激活”并发出“点击”(在主分支中,“映射器”功能不再存在)。