ServiceWorkerRegistration: pushManager 屬性
注意:此功能在 Web Workers 中可用。
ServiceWorkerRegistration 介面的只讀屬性 pushManager 返回一個指向 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);
},
);
});
規範
| 規範 |
|---|
| 推送 API # dom-serviceworkerregistration-pushmanager |
瀏覽器相容性
載入中…