doğukan
doğukan

Reputation: 27451

How can I listen click event on Windows notifications?

I'm simply send notifications using the node-notifier package. Also, when I click on the notification, it has to go to a link. But I can't listen the click event. The events provided by the package do nothing. 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:

enter image description here

I can use any other package. I just want to listen click event.

@user120242's answer works but does not works clicking after the notification disappears. Is there any way? I added a gif.

Upvotes: 12

Views: 1884

Answers (1)

user120242
user120242

Reputation: 15268

Action Center requires separate implementation in native code, which node-notifier doesn't have. You can try node-powertoast instead: 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. 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
click not firing is affecting many people starting from after version 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.

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/lib/utils.js#L245
Patch those to account for 'activate' and emit 'click' (in master branch the "mapper" function no longer exists).

Upvotes: 7

Related Questions