GCM webpush in background

2019-07-10 02:48发布

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条回答
来,给爷笑一个
2楼-- · 2019-07-10 03:28
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', {});
          });
      });
  );
}
查看更多
登录 后发表回答