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.
source share