12

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.

doğukan
  • 23,073
  • 13
  • 57
  • 69

1 Answers1

7

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

user120242
  • 14,918
  • 3
  • 38
  • 52
  • So, is there any way it will work when clicked after the notification disappears? I added a gif. – doğukan Jun 06 '20 at 20:44
  • It would have to be implemented in the native code for node-notifier. It's not a small task, so it probably won't happen any time soon with node-notifier https://github.com/xan105/node-powertoast will work – user120242 Jun 06 '20 at 21:02
  • @dgknca if it worked out for you, would you award the bounty here? Note: if you don't award it, the [bounty](https://stackoverflow.com/help/bounty) is **non-refundable** so the 50 points will just disappear. I'll receive it anyways, because of the [expiry rules](https://stackoverflow.com/help/bounty), but I feel as though as a personal choice it's better if you award it yourself – user120242 Jun 13 '20 at 17:16
  • You will earn the 50 rep. I'm don't give because the question remains among the featured questions and reaches more people. "If you do not award your bounty within 7 days (plus the grace period), the highest voted answer created after the bounty started with a minimum score of 2 will be awarded half the bounty amount (or the full amount, if the answer is also accepted). If two or more eligible answers have the same score (their scores are tied), the oldest answer is chosen. If there's no answer meeting those criteria, no bounty is awarded to anyone." – doğukan Jun 13 '20 at 17:28
  • @dgknca Ah okay. Ya I read the expiry rules. But looks like you actually understand them better than I do. Didn't know it mattered for exposure (and seems like people actually do care about this issue). That's kind of cool, it's almost like automatic promotion. Just thought it would be good for you to know, "I will lose these points anyways, and I'd like to award them on my own terms" – user120242 Jun 13 '20 at 17:30