Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome Silent Push Notifications

I've been reading through the docs for Chrome's implementation of the Web Push API here, and I noticed the API says "you promise to show a notification whenever you receive a push" and under limitations it's stated "you have to show a notification when you receive a push message".

After implementing the example on my localhost, I used cURL to send a push notification successfully. I was curious, so I commented out the lines that actually call the showNotification function, and put in a console.log instead and found that I could, in fact, send, receive, and totally ignore a push notification. I even tried using an if-statement to control whether or not to show them based on global boolean that I controlled from my main page, and that worked. So I was wondering if anyone knew what they meant by saying you need to show a notification, and that silent push notifications weren't available?

This wasn't just for the heck of it, I legitimately may need to control whether or not to show these notifications in my web app, so it would be great if this were actually possible. Code below in case you're curious.

self.addEventListener('push', function(event) {
  var title = 'New Message';
  var body = 'You have received a new message!';
  var icon = '/img/favicon.png';
  var tag = 'well-notification';
  console.log("DID RECEIVE NOTIFICATION")

  if(settingsShowNotification) {
    event.waitUntil(
      self.registration.showNotification(title, {
         body: body,
         icon: icon,
         tag: tag
      })
    );
  }
});

EDIT: On Chrome 47, if it's relevant.

UPDATE: After further experimenting, I found the obvious issue that I can't update the original global variable once the user navigates away and then re-navigates to the same page. However, I was able to circumvent this using a variable on the serviceworker itself and sending a message to the service worker using the API described here to toggle the showNotifications boolean.

like image 965
Ruben Martinez Jr. Avatar asked Oct 12 '15 23:10

Ruben Martinez Jr.


People also ask

How do I manage Chrome push notifications?

Go to Settings > Privacy and Security > Site Settings, then scroll down to Notifications in the pop-up window that appears. From there, you can toggle the Sites Can Ask to Send Notifications switch that turns website notification prompts on or off.

How do I permanently turn off these annoying Web push notifications?

Disable Notifications in Chrome If you want to turn off these messages completely, select Don't allow sites to send notifications. This will stop all push notifications from your browser, but this will also include productivity-related notifications from apps like Gmail and Google Meet.


1 Answers

You do have to show a notification, and if you don't show a notification you get a forced notification from the browser saying "This site has been updated in the background". But the requirements that show the scary message have been relaxed slightly:

As of Jan. '16, it seems like up to the last 10 notifications are checked for whether each showed a notification or not. If one notification in the last ten notifications did not show a notification, that's considered an accident and the browser won't show the scary "This site has been updated in the background". You have to miss two notifications in the last ten for the scary message to appear.

Note: If the URL in the address bar of the active browser tab matches the origin of your page, and the browser is not minimized, you are not required to show a notification. This is probably why your tests succeeded, if you were on the page itself while running your tests.

Chromium bug that tracks the implementation: https://code.google.com/p/chromium/issues/detail?id=437277

Relevant lines of source code: https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/push_messaging/push_messaging_notification_manager.cc&l=249

like image 184
Jason Avatar answered Sep 28 '22 01:09

Jason