此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

ServiceWorkerRegistration.pushManager

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2023年3月⁩.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

ServiceWorkerRegistration 接口的 pushManager 属性返回用于管理推送订阅的 PushManager 接口的引用。包括支持订阅,获取活动订阅和访问推送权限状态。

一个 PushManager 对象。

示例

js
this.onpush = function (event) {
  console.log(event.data);
  // From here we can write the data to IndexedDB, send it to any open
  // windows, display a notification, etc.
};

navigator.serviceWorker
  .register("serviceworker.js")
  .then(function (serviceWorkerRegistration) {
    serviceWorkerRegistration.pushManager.subscribe().then(
      function (pushSubscription) {
        console.log(pushSubscription.subscriptionId);
        console.log(pushSubscription.endpoint);
        // The push subscription details needed by the application
        // server are now available, and can be sent to it using,
        // for example, an XMLHttpRequest.
      },
      function (error) {
        // During development it often helps to log errors to the
        // console. In a production environment it might make sense to
        // also report information about errors back to the
        // application server.
        console.log(error);
      },
    );
  });

规范

Specification
Push API
# dom-serviceworkerregistration-pushmanager

浏览器兼容性

参见