--- title: PushManager slug: Web/API/PushManager tags: - API - Experimental - Interface - NeedsTranslation - Push - Push API - Reference - Service Workers - TopicStub translation_of: Web/API/PushManager --- <p>{{ApiRef("Push API")}}</p> <p><a href="/en-US/docs/Web/API/Push_API">Push API</a> 的<code>PushManager</code>接口提供了从第三方服务器接收消息通知的能力。</p> <p>可以通过ServiceWorkerRegistration.pushManager属性获得</p> <div class="note"> <p><strong>注意</strong>: 这个属性替代了已被废弃的{{domxref("PushRegistrationManager")}}</p> </div> <h2 id="Properties">Properties</h2> <p><em>None.</em></p> <h2 id="Methods">Methods</h2> <dl> <dt>{{domxref("PushManager.getSubscription()")}}</dt> <dd>用于获取已经存在的push订阅。返回一个{{jsxref("Promise")}},这个{{jsxref("Promise")}}包装着push订阅信息的{{domxref("PushSubscription")}}对象。如果没有已经存在的订阅,则返回<code>null</code>。</dd> <dt>{{domxref("PushManager.permissionState()")}}</dt> <dd>返回一个{{jsxref("Promise")}},标识这当前{{domxref("PushManager")}}的权限状态,只能是<code>'granted'<font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">,</span></font></code><code>'denied'</code> ,<code>'prompt'</code>中的一种。</dd> <dt>{{domxref("PushManager.subscribe()")}}</dt> <dd>向push服务器(即第三方push server)发起订阅。返回一个{{jsxref("Promise")}},这个{{jsxref("Promise")}}包装着push订阅信息的{{domxref("PushSubscription")}}对象。如果当前的service worke没有已经存在的订阅,则会创建一个新的push订阅。</dd> </dl> <h3 id="已废弃的方法">已废弃的方法</h3> <dl> <dt>{{domxref("PushManager.hasPermission()")}} {{deprecated_inline}}</dt> <dd>(已废弃)返回一个{{jsxref("Promise")}},标识着该webapp的<code>PushPermissionStatus</code>状态,该状态只能是<code>'granted'<font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">,</span></font></code><code>'denied'</code> ,<code>'default'</code>中的一种。目前已经被{{domxref("PushManager.permissionState()")}}取代。</dd> <dt>{{domxref("PushManager.register()")}} {{deprecated_inline}}</dt> <dd>(已废弃)发起注册push订阅。目前已经被{{domxref("PushManager.subscribe()")}}取代。</dd> <dt>{{domxref("PushManager.registrations()")}} {{deprecated_inline}}</dt> <dd>(已废弃)返回已存在的push订阅信息。目前已经被{{domxref("PushManager.getSubscription()")}}取代。</dd> <dt>{{domxref("PushManager.unregister()")}} {{deprecated_inline}}</dt> <dd>(已废弃)取消注册并删除指定的注册信息。在更新后的API中,请使用 {{domxref("PushSubscription.unsubscribe()")}}方法取消注册。</dd> </dl> <h2 id="示例">示例</h2> <pre class="brush: js">this.onpush = function(event) { console.log(event.data); // 这里我们可以将数据写入IndexedDB,发送给其他window对象,或者显示一个通知 } navigator.serviceWorker.register('serviceworker.js').then( function(serviceWorkerRegistration) { serviceWorkerRegistration.pushManager.subscribe().then( function(pushSubscription) { console.log(pushSubscription.subscriptionId); console.log(pushSubscription.endpoint); // 现在我们已经获取到了服务器需要的push订阅信息,我们可以使用XHR将它们发送给服务器 }, function(error) { // 在开发环境下打印错误是很有帮助的。在生产环境下,将错误上报到服务器也是十分必要的 console.log(error); } ); });</pre> <h2 id="Specifications">Specifications</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('Push API','#pushmanager-interface','PushManager')}}</td> <td>{{Spec2('Push API')}}</td> <td>Initial definition.</td> </tr> </tbody> </table> <h2 id="Browser_compatibility">Browser compatibility</h2> <p>{{Compat("api.PushManager")}}</p> <h2 id="See_also">See also</h2> <ul> <li><a href="/en-US/docs/Web/API/Push_API/Using_the_Push_API">Using the Push API</a></li> <li><a href="/en-US/docs/Web/API/Push_API">Push API</a></li> <li><a href="/en-US/docs/Web/API/Service_Worker_API">Service Worker API</a></li> </ul>