In my current architecture, I submit push notifications using the GCM API.
This triggers a 'push' event in the service worker. The service worker then adds some data to the notification.
If the service worker is unable to add the data, due to some error, I am getting a blank notification.
Can service workers avoid showing notifications sent by GCM?
From the Push Notifications guide at Google Developers:
You must pass a {userVisibleOnly: true}
argument to the subscribe()
method. This tells the browser that a notification will always be
shown when a push message is received. Currently it’s mandatory to
show a notification.
The upshot is that if you don't show something in response to a push (after promising the browser that you will), the browser (Chrome, at any rate) will just show something for you. However, it seems likely that this will change at some point.
It seems that upon the error condition you hit you are still showing your own notification anyway and since you could not fill it up it shows up blank.
Your best option is to fallback to some generic text. You can risk not showing it but after a few times chrome will start showing its own notification (after all you "promised" you would be showing one by saying {userVisibleOnly: true})
The best sequence of steps I can think of looking at the chrome code is:
1) Try to show the notification you want to show
2) If that fails for whatever reason check if you are in the foreground or if one of your notifications is already showing. If so do nothing
3) Else you need to show a notification, otherwise it's likely that chrome will show it for you and risking it is probably not worth it. Just add some generic text and show it.