最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - chrome notifications unsubscribe event - Stack Overflow

programmeradmin3浏览0评论

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

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 ?

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

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 ?

Share Improve this question edited Apr 27, 2018 at 18:59 Tarun Lalwani 147k11 gold badges213 silver badges276 bronze badges asked Feb 11, 2018 at 8:02 lior rlior r 2,3007 gold badges44 silver badges82 bronze badges 4
  • Hi, I'm also looking to this because to many entries in DB, after manualy subscribing back new entry is created. – SergkeiM Commented Apr 27, 2018 at 11:24
  • When I go to chrome://settings/content/notifications and remove your site’s permission to show notifications from there, I don’t even need to currently have your site itself loaded in my browser at that point ... so I don’t see anyone who could catch that event even if it existed in that situation ... – C3roe Commented Apr 27, 2018 at 12:11
  • @CBroe Hi thank you for reply, but in this case how can I delete the endpoint attached to the user in my DB? because next time user will allow notifications I'm generating a new Endpoint, this makes 2 endpoint for the user (because they are always different). – SergkeiM Commented Apr 27, 2018 at 13:48
  • 1 May be this can help How to listen for web notification permission change – anshulk Commented May 1, 2018 at 16:02
Add a ment  | 

2 Answers 2

Reset to default 3

Your looking for the pushsubscriptionchange event

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

Unsubscribe is browser setting that can be done by settings tab and offline mode so you cannot get that information

But Once the user has unsubscribed (i.e. deleted the service worker registration), then on sending the next message you should get a failure message from the FCM API.(if you are using firebase)

So think in this way .... :)

发布评论

评论列表(0)

  1. 暂无评论