Push APIservice
The pushsubscriptionchange event is sent to the global scope of a ServiceWorker to indicate a change in push subscription that was triggered outside the application's control.
This may occur if the subscription was refreshed by the browser, but it may also happen if the subscription has been revoked or lost.
This event is not cancelable and does not bubble.
Syntax
Use the event name in EventTarget/addEventListener:
addEventListener("pushsubscriptionchange", (event) => { })
Alternatively, assign to the event handler property of the interface:
onpushsubscriptionchange = (event) => { }
Event type
A generic Event.
Usage notes
Although examples demonstrating how to share subscription related information with the application server tend to use WorkerGlobalScope/fetch, this is not necessarily the best choice for real-world use, since it will not work if the app is offline, for example.
Consider using another method to synchronize subscription information between your service worker and the app server, or make sure your code using fetch() is robust enough to handle cases where attempts to exchange data fail.
[!NOTE] In earlier drafts of the specification, this event was defined to be sent when a PushSubscription has expired.
Examples
This example, run in the context of a service worker, listens for a pushsubscriptionchange event and re-subscribes to the lapsed subscription.
self.addEventListener("pushsubscriptionchange", (event) => {
const conv = (val) =>
self.btoa(String.fromCharCode.apply(null, new Uint8Array(val)));
const getPayload = (subscription) => ({
endpoint: subscription.endpoint,
publicKey: conv(subscription.getKey("p256dh")),
authToken: conv(subscription.getKey("auth")),
});
const subscription = self.registration.pushManager
.subscribe(event.oldSubscription.options)
.then((subscription) =>
fetch("register", {
method: "post",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({
old: getPayload(event.oldSubscription),
new: getPayload(subscription),
}),
}),
);
event.waitUntil(subscription);
});
When a pushsubscriptionchange event arrives, indicating that the subscription has expired, we resubscribe by calling the push manager's PushManager.subscribe method. When the returned promise is resolved, we receive the new subscription. This is delivered to the app server using a WorkerGlobalScope/fetch call to post a JSON formatted rendition of the subscription's PushSubscription.endpoint to the app server.
You can also use the onpushsubscriptionchange event handler property to set up the event handler (but note that this replaces other event listeners added using onpushsubscriptionchange, if any):
self.onpushsubscriptionchange = (event) => {
event.waitUntil(
self.registration.pushManager
.subscribe(event.oldSubscription.options)
.then((subscription) => {
/* ... */
}),
);
};
Specifications
Browser compatibility
See also
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
