Can service-worker stop push notification sent by GCM api?

删除回忆录丶 提交于 2019-12-12 18:07:09

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/35202105/can-service-worker-stop-push-notification-sent-by-gcm-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!