Google Chrome Silent Push Notifications

I read through documents for implementing Chrome’s Web Push API here , and I noticed that the API says: “You promise to show a notification when you receive a push,” and in accordance with the restrictions you specify, you have to show a notification when you receive a push message. "

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

It was not just for this, I can quite legitimately control whether or not to show these notifications in my web application, so it would be great if it were really possible. The code is below if you are interested.

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: In Chrome 47, if that matters.

UPDATE: after further experimentation, I discovered an obvious problem that I can’t update the original global variable after the user moves around and then goes back to the same page again. However, I managed to get around this by using a variable in the service itself and sending a message to the service employee using the API described here to switch showNotifications boolean.

+5
source share
1 answer

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

As of January '16, it seems that prior to the last 10 notifications, they are checked to see if they showed each notification or not. If one of the notifications in the last ten notifications did not show notifications that were considered as an accident, and the browser does not show the scary "This site has been updated in the background." You must skip two notifications in the last ten so that a scary message appears.

Note. If the URL in the address bar of the active tab of the browser matches the top of your page, and the browser is not minimized, you do not need to show a notification. This is probably why your tests were successful if you were on the page itself while you ran your tests.

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

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

+7
source

All Articles