Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 857
doğukan Avatar asked Jun 04 '20 11:06

doğukan


People also ask

How do I read Windows notifications?

Windows 10 puts notifications and quick actions in Action Center—right on the taskbar—where you can get to them instantly. Select Action Center on the taskbar to open it. (You can also swipe in from the right edge of your screen, or press Windows logo key + A.)

What is notification listener?

android.service.notification.NotificationListenerService. A service that receives calls from the system when new notifications are posted or removed, or their ranking changed.

How do I manage Windows notifications?

Manage notifications Windows 10: Select Start > Settings > System > Notifications & actions. Manage notifications MacOS: Select Apple menu > System Preferences > Notifications. Manage notifications Android: Select your phone's Settings app > Apps & Notifications>

What are Windows toast notifications?

A toast notification is a message that your app can construct and deliver to your user while they are not currently inside your app. This quickstart walks you through the steps to create, deliver, and display a Windows 10 or Windows 11 toast notification using rich content and interactive actions.


1 Answers

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

like image 133
user120242 Avatar answered Oct 14 '22 07:10

user120242