Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notificationclick does not fire in chrome even though registered in addEventListener in serviceworker.js

I'm enabling web notification to my web site and added below event listener to service worker .js file, This works fine in edge and click event gets trigger and opens a new window, however chrome nothing happens and event it self does not trigger -

self.addEventListener('notificationclick', function (event) {
  event.notification.close();
  console.log('Notification notificationclick triggered');
  event.waitUntil(
    clients.openWindow(event.notification.data)
  );
})
like image 218
dewraj singh Avatar asked Jan 06 '19 18:01

dewraj singh


2 Answers

Just tested it on Chrome and its working as intended:

self.addEventListener('notificationclick', function (event) {
  clients.openWindow("/");
});

Unregister your Service Worker using the Chrome devs window then find the Application tab. You would be able to update or unregister that particular service.

Looks like you need to do that in order to see the changes you made to the file from time to time. Maybe browser cache or so.

like image 172
Solrac Avatar answered Oct 03 '22 15:10

Solrac


You can use data object in place of notification.

eg.

  {
   "to": "pass token here",
   "data":{
    "title": "Hello",
    "body": "Good Evening",
    "click_action":"https://stackoverflow.com/"
      }
  }
like image 38
Maheshvirus Avatar answered Oct 03 '22 16:10

Maheshvirus