Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

PushManager.subscribe()

这篇翻译不完整。请帮忙从英语翻译这篇文章

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

PushManager  的 subscribe() 方法订阅了一个推送服务。

返回一个 Promise,包含推送订阅详情的 PushSubscription 对象。如果当前 service worker 不存在订阅时会创建一个新的推送订阅。

语法

​PushManager.subscribe(options).then(function(pushSubscription) { ... } );

参数

options 可选
一个包含可选配置参数的对象。拥有以下属性:
  • userVisibleOnly: 布尔值,表示返回的推送订阅将只被用于对用户可见的消息。

返回值

返回 PushSubscription 对象的 Promise

示例

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

规范

Specification Status Comment
Push API
subscribe()
Working Draft Initial definition.

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 42.0 44.0 (44.0)[1] 未实现 ? 未实现
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support 未实现 未实现 48.0 (48)[2] 未实现 未实现 未实现 未实现 42.0

另见

文档标签和贡献者

 此页面的贡献者: jiraiya
 最后编辑者: jiraiya,