DEV Community

Discussion on: Web Push Notification with web-push | Angular & Node JS

Collapse
 
ratfou profile image
RatFou

Hi, You don't use "swPush.notificationClicks" to handle click?

Collapse
 
devsmranjan profile image
Smruti Ranjan Rana

Yes you can use. Here I'm sending data with some default operations which are there in service worker file after you build your angular app.

Collapse
 
thegoke profile image
Kevin Camilo Gómez González

You can but when the webpage is open.

If the webpage isn't open you need to modify your service worker and add this (this only works for angular ngsw-worker.js file)

// line 1914
this.scope.addEventListener('notificationclick', (event) => {
event.notification.close();
// Get all the Window clients
event.waitUntil(clients.matchAll({ includeUncontrolled: true, type: 'window' }).then(clientsArr => {
// If a Window tab matching the targeted URL already exists, focus that;
const hadWindowToFocus = clientsArr.some(windowClient => { windowClient.url === event.notification.data[event.action].url ? (windowClient.focus(), true) : false });
// Otherwise, open a new tab to the applicable URL and focus it.
if (!hadWindowToFocus) {
clients.openWindow(event.notification.data[event.action].url).then(windowClient => {
if (windowClient) {
windowClient.focus();
console.log('[Service Worker] - Notification click Received.', event);
}
});
}
}));
this.onClick(event);
});

Collapse
 
thegoke profile image
Kevin Camilo Gómez González

sorry if i post code in comments, this is my first comment :)