How to properly unregister a service worker installed by Firebase

I am trying to implement a toggle to enable / disable notifications in a web app using Firebase messages. My notifications are working fine, but there is a problem when the user tries to turn off notifications. If I just unregistered the service worker with:

navigator.serviceWorker.getRegistrations().then((r) => {
  return Promise.all(r.map(reg => reg.unregister()));
});

      

then the worker does get unregistered data, but if the user tries to enable alerts again, Firebase will throw this error:

messaging/no-sw-in-reg

      

The error makes sense, but there seems to be no built-in methods to debug the Firebase account for users who no longer want to use it. Should you uninstall the firebase app and reinitialize it when / if the user asks for notifications again?

I understand that I could just delete the firebase token and forget about the user in my database, but I don't want to leave a useless service worker who will never be called again.

What's the correct way to give the user the ability to completely disable firewall notifications in your web app?

+3


source to share


2 answers


I had the same problem. So I use your code to get rid of the worker, and after that I re-create the messaging object so you can unsubscribe and install the service worker again without reloading the page.

I found out that this problem occurs (at least in my code) because the messaging object caches the device token, so it won't fire events that should be triggered when using the device token. Therefore, reinitializing the messenging object is a viable option when you don't want to reload the page. If someone can figure out how to clear the message files cache this would be an even better solution. Until then, here's my ...

Sample code:



navigator.serviceWorker.getRegistrations().then((r) => {
   return Promise.all(r.map(reg => reg.unregister()));
});
this.messaging = firebase.messaging();

      

Let the source be with you.

+1


source


In case it is useful to someone, instead of deactivating the entire ServiceWorker, you can only opt out of PushNotifications.

By unsubscribing from PushSubscription, you also avoid reassigning the messaging property.

This is how I unsubscribe:

/*
Returns a Promise that resolves on the PushSubscription object that controls the push notification subscription, after it finds the coresponding ServiceWorker
*/
getSubscription() : Promise<any>{
    if(!navigator) return;

    const subscriptions = navigator.serviceWorker.getRegistrations().then((r) => {
      return r.map((sw) =>{
        if(!(sw.active && sw.active.scriptURL.includes("firebase-messaging")))
          return Promise.resolve();
        return sw.pushManager.getSubscription();

      });
    });

    return subscriptions.then(arr => arr ? arr[0] : Promise.resolve(null));
  }

      



=============================================== === =========================

// Unsubscribes PushNotifications
rejectPermission() : Promise<boolean>{
    return this.getSubscription().then((pushSubscription : PushSubscription) =>{
      return pushSubscription.unsubscribe();
    });
  }

      

What I am doing for my application and everything seems to be working fine.

0


source







All Articles