PushManager

PushManager interface of the 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 that resolves to a PushSubscription object containing details of an existing subscription. If no existing subscription exists, this resolves to a null value.
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 that resolves to a 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);
      }
    );
  });
					

规范

规范 状态 Comment
Push API
The definition of 'PushManager' in that specification.
工作草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
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
注意事项
options 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

图例

完整支持
完整支持
不支持
不支持
实验。期望将来行为有所改变。
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
用户必须明确启用此特征。

另请参阅