Search code examples
javascriptgoogle-chromepush-notificationservice-workerweb-push

chrome notifications unsubscribe event


Is it possible to catch unsubscribe event ?

This is the code im using to subscribe the user to the notifications

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.ready.then(function(reg) {
      reg.pushManager.subscribe({
        userVisibleOnly: true
      }).then(function(sub) {
          add_endpoint_to_db( sub ); // im adding the user deatils to my db
      }).catch(function(e) {
        if (Notification.permission === 'denied') {

        } else {

        }
      });
  });

}

In case the user chooses to manually remove the notifications

Notification removal

I want to catch the action in order to remove his entry from the DB

So is it possible to catch the notifications change event ?


Solution

  • Your looking for the pushsubscriptionchange event

    self.addEventListener('pushsubscriptionchange', function() {
      // remove the entry from DB
    });