12

我只是使用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");
});

这是我的通知:

在此处输入图像描述

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

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

4

1 回答 1

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));

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


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

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://stackoverflow.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
修补这些以解决“激活”并发出“点击”(在主分支中,“映射器”功能不再存在)。

于 2020-06-06T19:03:35.380 回答