How should I handle the pushsubscriptionchange event?

房东的猫 提交于 2021-01-27 12:07:50

问题


According to the spec, a pushsubscriptionchange event indicates that "a push subscription has been invalidated, or will soon be invalidated." What are the best practices for handling this event?


回答1:


  1. Process the re-subscription entirely within the service worker. There may be no client (i.e. open pages) associated with the service worker at the point the event is fired--do not use postMessage() to trigger a re-subscription from the client.
  2. Implementations are likely to honour the old subscription for a period of time (probably days). Whilst the spec makes no such guarantee, it is likely that implementations will support both old and new keys for some time to reduce/eliminate the chance of messages being dropped because one of the clients or servers involved hasn't received the "new" key.
  3. As of April 2016 neither Chrome nor Firefox fires this event. However, since it's in the spec, to ensure users continue to receive messages, you should make sure you handle it correctly.



回答2:


You can find here a fully detailed explanation of handling pushsubscriptionchange event with some pitfalls you will have to deal with. Like:

  • Push subscription change is not currently implemented in Chrome.
  • PushSubscriptionChangeEvent is always null. You won't have the oldSubscription to use if you want to replace it in the server.

How to handle WebPush API PushSubscriptionChange event in modern browsers



来源:https://stackoverflow.com/questions/36602095/how-should-i-handle-the-pushsubscriptionchange-event

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