ServiceWorkerRegistration.pushManager
Baseline 2023Newly available
Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
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 |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
pushManager |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
- See implementation notes.
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.