简体   繁体   English

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

[英]How can I listen click event on Windows notifications?

I'm simply send notifications using the node-notifier package.我只是使用节点通知器package 发送通知。 Also, when I click on the notification, it has to go to a link.另外,当我点击通知时,它必须以 go 为链接。 But I can't listen the click event.但我听不到点击事件。 The events provided by the package do nothing. package 提供的事件什么都不做。 This is my code:这是我的代码:

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

And this is my notification:这是我的通知:

在此处输入图像描述

I can use any other package.我可以使用任何其他 package。 I just want to listen click event.我只想听点击事件。

@user120242's answer works but does not works clicking after the notification disappears. @ user120242 的答案有效,但在通知消失后单击无效。 Is there any way?有什么办法吗? I added a gif .我添加了一个gif

Action Center requires separate implementation in native code, which node-notifier doesn't have. Action Center 需要在本机代码中单独实现,而 node-notifier 没有。 You can try node-powertoast instead: npm i node-powertoast您可以尝试使用 node-powertoast代替: npm i node-powertoast

const toast = require('powertoast');

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

Callback functions onActivate are also supported.还支持 onActivate 回调函数。 Check documentation in the link for more details.查看链接中的文档以获取更多详细信息。


How to fix node-notifier click event:如何修复节点通知点击事件:

https://github.com/mikaelbr/node-notifier/issues/291#issuecomment-555741924 https://github.com/mikaelbr/node-notifier/issues/291#issuecomment-555741924
click not firing is affecting many people starting from after version 5从版本 5 开始,点击不触发影响了许多人

Due to changes in the use of the name of the action not being consistent.由于更改使用的动作名称不一致。
You can rollback to 5.4.3, or use the suggestion of using the callback instead in the thread.您可以回滚到 5.4.3,或者在线程中使用使用回调的建议。

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

Or:或者:

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

Another possibility if you're confident and would rather fix the library itself: https://github.com/mikaelbr/node-notifier/blob/v5.4.3/notifiers/toaster.js#L63如果您有信心并且宁愿修复库本身,还有另一种可能性: 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 https://github.com/mikaelbr/node-notifier/blob/v5.4.3/lib/utils.js#L245
Patch those to account for 'activate' and emit 'click' (in master branch the "mapper" function no longer exists).修补这些以解决“激活”并发出“点击”(在主分支中,“映射器”function 不再存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM