pushsubscriptionchange event is sent to the global scope 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.

冒泡 No
可取消 No
接口 PushSubscriptionChangeEvent
事件处理程序特性 onpushsubscriptionchange

用法注意事项

Although examples demonstrating how to share subscription related information with the application server tend to use 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.

注意: In earlier drafts of the specification, this event was defined to be sent when a PushSubscription has expired.

范例

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 => {
  event.waitUntil(swRegistration.pushManager.subscribe(event.oldSubscription.options)
    .then(subscription => {
      return fetch("register", {
        method: "post",
        headers: {
          "Content-type": "application/json"
        },
        body: JSON.stringify({
          endpoint: subscription.endpoint
        })
      });
    })
  );
}, false);
					

pushsubscriptionchange event arrives, indicating that the subscription has expired, we resubscribe by calling the push manager's subscribe() method. When the returned promise is resolved, we receive the new subscription. This is delivered to the app server using a fetch() call to post a JSON formatted rendition of the subscription's endpoint to the app server.

还可以使用 onpushsubscriptionchange event handler property to set up the event handler:

self.onpushsubscriptionchange = event => {
  event.waitUntil(swRegistration.pushManager.subscribe(event.oldSubscription.options)
    .then(subscription => {
      /* ... */
    )
};
					

规范

规范 状态 注释
Push API
The definition of 'pushsubscriptionchange' in that specification.
工作草案 最初的规范。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
pushsubscriptionchange event Chrome 40 Edge ≤79 Firefox 44
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE No Opera 24 Safari 11.1 WebView Android 40 Chrome Android 40 Firefox Android 44 Opera Android 24 Safari iOS 11.3 Samsung Internet Android 4.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: