PushManager 接口在 Push API provides a way to receive notifications from third-party servers as well as request URLs for push notifications.

This interface is accessed via the ServiceWorkerRegistration.pushManager 特性。

注意 : This interface replaces functionality previously offered by the obsolete PushRegistrationManager 接口。

特性

PushManager.supportedContentEncodings

Returns an array of supported content codings that can be used to encrypt the payload of a push message.

方法

PushManager.getSubscription()
Retrieves an existing push subscription. It returns a Promise 解析为 PushSubscription object containing details of an existing subscription. If no existing subscription exists, this resolves to a null 值。
PushManager.permissionState()
返回 Promise that resolves to the permission state of the current PushManager , which will be one of 'granted' , 'denied' ,或 'prompt' .
PushManager.subscribe()
Subscribes to a push service. It returns a Promise 解析为 PushSubscription object containing details of a push subscription. A new push subscription is created if the current service worker does not have an existing subscription.

弃用方法

PushManager.hasPermission()
返回 Promise that resolves to the PushPermissionStatus of the requesting webapp, which will be one of granted , denied ,或 default . Replaced by PushManager.permissionState() .
PushManager.register()
Subscribes to a push subscription. Replaced by PushManager.subscribe() .
PushManager.registrations()
Retrieves existing push subscriptions. Replaced by PushManager.getSubscription() .
PushManager.unregister()
Unregisters and deletes a specified subscription endpoint. In the updated API, a subscription is unregistered by calling the PushSubscription.unsubscribe() 方法。

范例

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.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);
      }
    );
  });
					

规范

规范 状态 注释
Push API
The definition of 'PushManager' 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
PushManager Chrome 42 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
getSubscription Chrome 42 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
hasPermission Chrome 42 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
permissionState Chrome 42 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
register Chrome 42 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
registrations Chrome 42 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
subscribe Chrome 42 注意事项
42 注意事项
选项 parameter with a applicationServerKey value is required.
Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
From Firefox 72 onwards, can only be called in response to a user gesture such as a click 事件。
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0
supportedContentEncodings Chrome 60 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 47 Safari 不支持 No WebView Android 不支持 No Chrome Android 60 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 44 Safari iOS 不支持 No Samsung Internet Android 4.0
unregister Chrome 42 Edge 16 Disabled
16 Disabled
Disabled From version 16: this feature is behind the Enable service workers preference.
17
Firefox 44 注意事项
44 注意事项
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE 不支持 No Opera 29 Safari 不支持 No WebView Android 不支持 No Chrome Android 42 Firefox Android 48 注意事项
48 注意事项
Push enabled by default.
Opera Android 29 Safari iOS 不支持 No Samsung Internet Android 4.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. Push API
  2. PushManager
  3. 特性
    1. supportedContentEncodings
  4. 方法
    1. getSubscription()
    2. hasPermission()
    3. permissionState()
    4. register()
    5. registrations()
    6. subscribe()
    7. unregister()
  5. Related pages for Push API
    1. PushEvent
    2. PushMessageData
    3. PushSubscription
    4. ServiceWorkerGlobalScope.onpush
    5. ServiceWorkerGlobalScope.onpushsubscriptionchange
    6. ServiceWorkerRegistration.pushManager

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1