The goal is that when a user clicks on the browser notification, to look if the page is already open and go to it, and if not, open it in a new tab or window.
this is doable when using the native service worker using this code
const promiseChain = clients.matchAll({
type: 'window',
includeUncontrolled: true }) .then((windowClients) => {
let matchingClient = null;
for (let i = 0; i < windowClients.length; i++) {
const windowClient = windowClients[i];
if (windowClient.url === urlToOpen) {
matchingClient = windowClient;
break;
}
}
if (matchingClient) {
return matchingClient.focus();
} else {
return clients.openWindow(urlToOpen);
} });
event.waitUntil(promiseChain);
but in angular they have their own service worker wrapper, and it generates service worker file on run time.
my code in angular now is this
this.swPush.notificationClicks.subscribe(
({ action, notification }) => {
if (notification && notification !== null && notification['data']) {
const url = notification['data'].url;
window.open(url, '_blank');
}
});
So first is there a way to implement this in angular (swPush) ?
if no, do I have to edit the generated service worker file, and how (best practice) ? or can I import another service worker file ?