GCM webpush in background

两盒软妹~` 提交于 2019-12-11 03:56:07

问题


I implemented web push notifications. Steps to get a bug:

  1. Open website
  2. Subscribe for push notifications
  3. Send many pushes through gcm - all fine
  4. Close tab with site
  5. Send push and receive "double push" - first one is ok, second is "This site has been updated in background"
  6. Reopen website
  7. Send push - all fine

I know this can happen when the service worker receives push and doesn't show notification. But I see normal notification, why I also see other strange notification? Can I get rid such behaviour?


回答1:


self.addEventListener('push', function(event) {
  // this function should return promise always
}

In my case:

self.addEventListener('push', function(event) {
  event.waitUntil(
    self.registration.pushManager.getSubscription()
      .then(function(subscription) {
        fetch('url')
          .then(function(response) {
            self.registration.showNotification('title', {});
          });
      });
  );
}

should be:

self.addEventListener('push', function(event) {
  event.waitUntil(
    self.registration.pushManager.getSubscription()
      .then(function(subscription) {
        return fetch('url')
          .then(function(response) {
            return self.registration.showNotification('title', {});
          });
      });
  );
}


来源:https://stackoverflow.com/questions/32319307/gcm-webpush-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!