PWA Service worker notification click
The correct place for handling clicks of Service Worker-based notifications is in Service Worker. You need to move your listener - the part with self.addEventListener('notificationclick', ...)
- to your Service Worker code.
Note that you don't have window
access there. To navigate to the URL, you'd need to use clients.openWindow
instead.
So your app-side code will only have the reg.showNotification
call and your handler in the Service Worker will look like this:
self.addEventListener('notificationclick', function (event) {
event.notification.close();
clients.openWindow("https://youtu.be/PAvHeRGZ_lA");
});
Always remember, after adding code (refer answer ) in service-worker.js , restart your browser to reflect the changes. I was making change and not restarting browser window , got me frustrated !