1

I have implemented the browser push notification functionality and its working fine. I used this guide as the reference https://developers.google.com/web/fundamentals/getting-started/push-notifications/step-01?hl=en

However as payload is still not supported, I decided to query my server to get the notification data for each user which is also working fine.

There is one issue though. For some cases, after getting data from the server, I want to control whether to show the notification or not. I am not able to figure out how to do this. I tried returning false, throwing errors etc. But is always shows the default notification even if I don't call showNotification method. Let me know how to solve this. Following is the relevant code

    self.addEventListener('push', function(event) {
      event.waitUntil(
        fetch('/getPushNotificationData/').then(function(response){
          if (response.status !== 200) {  
            // I don't want to show any notification in this case  
            console.log('Looks like there was a problem. Status Code: ' + response.status);  
            throw new Error();  
          }
          return response.json().then(function(data){
            var shouldDisplay = data.shouldDisplay;
            if (shouldDisplay=='1'){
              var title = data.title;  
              var message = data.message;  
              var url = data.url;
              return self.registration.showNotification(title, {  
                  body: message,  
                  data: url  
                }); 
            }
            else{
              // I don't want to show any notification in this case also
              return true;
            }
          });
        })
        );
    });
Anurag
  • 1,521
  • 2
  • 17
  • 34
  • Possible duplicate of [Can service-worker stop push notification sent by GCM api?](http://stackoverflow.com/questions/35202105/can-service-worker-stop-push-notification-sent-by-gcm-api) – Brendan Ritchie Mar 01 '16 at 13:49
  • You cannot prevent the display of this notification. [See my answer here](http://stackoverflow.com/a/35203881/2993617). – Brendan Ritchie Mar 01 '16 at 13:52
  • ok, thanks for the information. I guess I have to rely on backup notification in that case. Is there any release tracker for this feature or any other information on whether this feature is in pipeline or not? – Anurag Mar 01 '16 at 15:48
  • I don't know, but you could try asking at the [service worker spec discussion](https://github.com/slightlyoff/ServiceWorker) or the [notifications spec discussion](https://github.com/whatwg/notifications). – Brendan Ritchie Mar 01 '16 at 15:54
  • I would like a clarification on this to ensure that I understood from the above correctly. I there no way to redesign or replace the native browser prompt with a better looking popup? – manospro Dec 08 '16 at 11:04

0 Answers0