问题
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:
- 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. - 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.
- 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 theoldSubscription
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