How to update my service worker for existing web push subscribers?

36 Views Asked by At

I have a simple web app that works with web push and notification API, I use FCM for sending messages and a simple service worker in JS that shows notifications once received by browser.

My issues is that when I updated my service worker file, the changes were not reflected (I added support for image + icon, before I have been showing only icons in push notifications).

I read that service worker either updates in 24 hours automatically or when the last page using it is closed? (is this across all subscribers/users or just for this 1 particular subscriber/user?

It's now more than 24 hours and I cannot see any changes.

I now have about 1k subscribers and it seems that they are still not being shown the icon+image, how can I "force" them to update the service worker? Is this even possible remotely or they need to revisit my website again?

I read about .update() and .skipWaiting() methods, though I'm not sure how to apply them, especially when I need to have these changes to happen to existing subscribers.

As for sample here is my current firebase-messaging-sw.js:

self.addEventListener("push", (event) => {
    const notif = event.data.json().notification;
    event.waitUntil(self.registration.showNotification(notif.title, {
        body: notif.body,
        icon: notif.data.icon,
        image: notif.image,
        data: {
            url: notif.click_action
        }
    }));
});

self.addEventListener("notificationclick", (event) => {
    const clickedNotification = event.notification;
    clickedNotification.close();
    event.WaitUntil(clients.openWindow(event.notification.data.url));
});
0

There are 0 best solutions below