1

I am using the following code.

try {
    const registration = await navigator.serviceWorker.ready;
    const subscription = await registration.pushManager.getSubscription();
    await subscription.unsubscribe();
} catch(error) {
    throw error;
}

Even After this code executes successfully, When I refresh the page and log Notification.permission , it's still granted. But It should be default . Am I missing something ?

If user grants the permission for the first time, and then unsubscribes, Notification.permission never changes ?

1 Answer 1

1

To my understanding, after calling unsubscribe, the result should be "denied" according to this because it is an explicit user choice. Can you check please if the permission is correctly removed in settings after your code is executed? On chrome go to settings -> privacy -> content settings -> scroll down to notifications or open chrome://settings/content/notifications

If the notification is still granted try to replace your code for test purposes with Mozilla's example code and see if this works:

navigator.serviceWorker.ready.then(function(reg) {
  reg.pushManager.getSubscription().then(function(subscription) {
    subscription.unsubscribe().then(function(successful) {
      console.log("unsubscribed");
    }).catch(function(e) {
      console.log("Something went wrong");
    })
  })        
});

The spec (draft) says, that "a push subscription is removed when service worker registration is cleared."

Try to additionally unregister the serviceworker and see if this solves the problem:

registration.unregister().then(function(boolean) {
      // if boolean = true, unregister is successful
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.