ServiceWorkerRegistration: pushManager 属性
注意:此功能在Web 工作线程中可用。
pushManager
是ServiceWorkerRegistration
接口的只读属性,它返回对PushManager
接口的引用,用于管理推送订阅;这包括对订阅、获取活动订阅和访问推送权限状态的支持。
值
一个PushManager
对象。
示例
js
this.onpush = (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((serviceWorkerRegistration) => {
serviceWorkerRegistration.pushManager.subscribe().then(
(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, the fetch() API.
},
(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.error(error);
},
);
});
规范
规范 |
---|
Push API # dom-serviceworkerregistration-pushmanager |
浏览器兼容性
BCD 表仅在启用 JavaScript 的浏览器中加载。