user2046649
user2046649

Reputation: 3

Google chrome web push api bug

What is this bug? When sending web pushing browser Google Chrome "sometimes" gives a second message with the text: "This site has been updated in the background." enter image description here

I want to make it only one message enter image description here

This text I found in source Chrome This site has been updated in the background. github.com/scheib/chromium/blob/master/chrome/app/resources/generated_resources_en-GB.хтб

How to get rid of this message.

Upvotes: 0

Views: 366

Answers (2)

Matt Gaunt
Matt Gaunt

Reputation: 9821

The reason this often occurs is the promise returned to event.waitUntil() didn't resolve with a notification being shown.

An example that might show the default push notification:

function handlePush() {
  // BAD: The fetch's promise isn't returned
  fetch('/some/api')
  .then(function(response) {
    return response.json();
  })
  .then(function(data) {
    // BAD: the showNotification promise isn't returned
    showNotification(data.title, {body: data.body});
  });
}

self.addEventListener(function(event) {
   event.waitUntil(handlePush());
});

Instead you could should write this as:

function handlePush() {
  // GOOD
  return fetch('/some/api')
  .then(function(response) {
    return response.json();
  })
  .then(function(data) {
    // GOOD
    return showNotification(data.title, {body: data.body});
  });
}

self.addEventListener(function(event) {
   const myNotificationPromise = handlePush();
   event.waitUntil(myNotificationPromise);
});

The reason this is all important is that browsers wait for the promise passed into event.waitUntil to resolve / finish so they know the service worker needs to be kept alive and running.

When the promise resolves for a push event, chrome will check that a notification has been shown and it falls into a race condition / specific circumstance as to whether Chrome shows this notification or not. Best bet is to ensure you have a correct promise chain.

I put some extra notes on promises on this post (See: 'Side Quest: Promises' https://gauntface.com/blog/2016/05/01/push-debugging-analytics)

Upvotes: 0

fgorski
fgorski

Reputation: 231

The way it works is a feature not a bug. Here is an issue that explains your situation in Chrome: https://code.google.com/p/chromium/issues/detail?id=437277

And more specific code comment in Chromium code: https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/push_messaging/push_messaging_notification_manager.cc&rcl=1449664275&l=287

What might have happened is some of the push messages sent to the client did not result in showing a notification.

Hope that helps

Upvotes: 0

Related Questions