Search code examples
chrome-gcmweb-pushpush-api

GCM webpush in background


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?


Solution

  • 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', {});
              });
          });
      );
    }