diff options
Diffstat (limited to 'files/zh-cn/web/api/pushmanager')
4 files changed, 435 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/pushmanager/getsubscription/index.html b/files/zh-cn/web/api/pushmanager/getsubscription/index.html new file mode 100644 index 0000000000..1a86444b2d --- /dev/null +++ b/files/zh-cn/web/api/pushmanager/getsubscription/index.html @@ -0,0 +1,95 @@ +--- +title: PushManager.getSubscription() +slug: Web/API/PushManager/getSubscription +tags: + - API + - PushManager + - Service Worker + - 实验中的 + - 方法 +translation_of: Web/API/PushManager/getSubscription +--- +<p>{{SeeCompatTable}}{{ApiRef("Push API")}}</p> + +<p>{{domxref("PushManager")}} 接口的方法<strong><code>PushManager.getSubscription()</code></strong> 尝试获取已有的推送订阅。</p> + +<p>它返回一个 {{jsxref("Promise")}} 用来resolve出一个包含现有订阅的详细信息的{{domxref("PushSubscription")}} 对象。如果不存在已有的推送订阅,返回null。</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js notranslate" style="font-size: 14px;">PushManager.getSubscription().then(function(pushSubscription) { ... } );</pre> + +<h3 id="参数">参数</h3> + +<p>无。</p> + +<h3 id="返回值">返回值</h3> + +<p>A {{jsxref("Promise")}} that resolves to a {{domxref("PushSubscription")}} object or <code>null</code>.</p> + +<h2 id="例子">例子</h2> + +<p>这个代码片段来自 <a href="https://github.com/GoogleChrome/samples/blob/gh-pages/push-messaging-and-notifications">push messaging and notification sample</a>. (没有能直接运行的例子.)</p> + +<pre class="brush: js notranslate" style="font-size: 14px;">// We need the service worker registration to check for a subscription + navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) { + // Do we already have a push message subscription? + serviceWorkerRegistration.pushManager.getSubscription() + .then(function(subscription) { + // Enable any UI which subscribes / unsubscribes from + // push messages. + var pushButton = document.querySelector('.js-push-button'); + pushButton.disabled = false; + + if (!subscription) { + // We aren’t subscribed to push, so set UI + // to allow the user to enable push + return; + } + + // Keep your server in sync with the latest subscriptionId + sendSubscriptionToServer(subscription); + + showCurlCommand(subscription); + + // Set your UI to show they have subscribed for + // push messages + pushButton.textContent = 'Disable Push Messages'; + isPushEnabled = true; + }) + .catch(function(err) { + window.Demo.debug.log('Error during getSubscription()', err); + }); + }); +</pre> + +<h2 id="说明">说明</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">说明</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + <tr> + <td>{{SpecName('Push API', '#widl-PushManager-getSubscription-Promise-PushSubscription', 'getSubscription()')}}</td> + <td>{{Spec2('Push API')}}</td> + <td>最初的定义</td> + </tr> + </tbody> +</table> + +<h2 id="支持的浏览器">支持的浏览器</h2> + +<div> +<div> +<div class="hidden">这个支持的浏览器列表是由结构化数据生成的。如果你想修改这个表格,请获取最新的代码并提交t<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 。</div> + +<p>{{Compat("api.PushManager.getSubscription")}}</p> +</div> +</div> + +<div id="gtx-trans" style="position: absolute; left: 488px; top: 144px;"> +<div class="gtx-trans-icon"></div> +</div> diff --git a/files/zh-cn/web/api/pushmanager/index.html b/files/zh-cn/web/api/pushmanager/index.html new file mode 100644 index 0000000000..28ba565023 --- /dev/null +++ b/files/zh-cn/web/api/pushmanager/index.html @@ -0,0 +1,159 @@ +--- +title: PushManager +slug: Web/API/PushManager +tags: + - API + - Experimental + - Interface + - NeedsTranslation + - Push + - Push API + - Reference + - Service Workers + - TopicStub +translation_of: Web/API/PushManager +--- +<p>{{SeeCompatTable}}{{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> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(42.0)}}</td> + <td>{{CompatGeckoDesktop(44.0)}}<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile(48)}}<sup>[2]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(42.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<ul> + <li>[1] Push (and <a href="/en-US/docs/Web/API/Service_Worker_API">Service Workers</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 Extended Support Release</a> (ESR.)</li> + <li>[2] Push has been enabled by default on Firefox for Android version 48.</li> +</ul> + +<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> diff --git a/files/zh-cn/web/api/pushmanager/subscribe/index.html b/files/zh-cn/web/api/pushmanager/subscribe/index.html new file mode 100644 index 0000000000..0e0f4c12e0 --- /dev/null +++ b/files/zh-cn/web/api/pushmanager/subscribe/index.html @@ -0,0 +1,138 @@ +--- +title: PushManager.subscribe() +slug: Web/API/PushManager/subscribe +translation_of: Web/API/PushManager/subscribe +--- +<p>{{SeeCompatTable}}{{ApiRef("Push API")}}</p> + +<p>{{domxref("PushManager")}} 的 <code><strong>subscribe() </strong></code>接口订阅了一个推送服务。</p> + +<p>返回一个 {{jsxref("Promise")}} 形式的 {{domxref("PushSubscription")}} 对象,该对象包含了推送订阅详情。如果当前 service worker 没有已存在的订阅,则会创建一个新的推送订阅。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">PushManager.subscribe(options).then(function(pushSubscription) { ... } );</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>options {{optional_inline}}</code></dt> + <dd><code>一个包含可选配置参数的对象。包含以下属性:</code> + <ul> + <li><code>userVisibleOnly</code>: 布尔值,表示返回的推送订阅将只能被用于对用户可见的消息。</li> + <li>applicationServerKey:推送服务器用来向客户端应用发送消息的公钥。该值是应用程序服务器生成的签名密钥对的一部分,可使用在 P-256 曲线上实现的椭圆曲线数字签名(ECDSA)。可以是{{domxref("DOMString")}} 或 {{domxref("ArrayBuffer")}}。</li> + </ul> + </dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>返回 {{domxref("PushSubscription")}} 对象的 {{jsxref("Promise")}}。</p> + +<h2 id="示例">示例</h2> + +<pre><code>this.onpush = function(event) { + console.log(event.data); + // </code>这里可以向 IndexDB 写入数据,向任何打开的窗口发送数据以及显示通知等<code> +} + +navigator.serviceWorker.register('serviceworker.js').then( + function(serviceWorkerRegistration) { + var options = { + userVisibleOnly: true, + applicationServerKey: applicationServerKey + }; + serviceWorkerRegistration.pushManager.subscribe(options).then( + function(pushSubscription) { + console.log(pushSubscription.endpoint); + // 应用服务器所需的推送订阅详情现在可用,并且可以通过如 XMLHttpRequest 的方式发送 + }, function(error) { + // 开发过程中将错误打印到控制台通常很有帮助。同样,生产环境下将错误信息发送至应用服务器后台也一样。 + console.log(error); + } + ); + });</code></pre> + +<h2 id="规范">规范</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', '#widl-PushManager-subscribe-Promise-PushSubscription--PushSubscriptionOptions-options', 'subscribe()')}}</td> + <td>{{Spec2('Push API')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(42.0)}}</td> + <td>{{CompatGeckoDesktop(44.0)}}<sup>[1]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile(48)}}<sup>[2]</sup></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(42.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<ul> + <li>[1] Push (and <a href="/en-US/docs/Web/API/Service_Worker_API">Service Workers</a>) have been disabled in the <a href="https://www.mozilla.org/en-US/firefox/organizations/">Firefox 45 Extended Support Release</a> (ESR.)</li> + <li>[2] Push has been enabled by default on Firefox for Android version 48.</li> +</ul> + +<h2 id="另见">另见</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/Push_API/Using_the_Push_API">使用 Push API</a></li> +</ul> diff --git a/files/zh-cn/web/api/pushmanager/supportedcontentencodings/index.html b/files/zh-cn/web/api/pushmanager/supportedcontentencodings/index.html new file mode 100644 index 0000000000..5b8540dd96 --- /dev/null +++ b/files/zh-cn/web/api/pushmanager/supportedcontentencodings/index.html @@ -0,0 +1,43 @@ +--- +title: PushManager.supportedContentEncodings +slug: Web/API/PushManager/supportedContentEncodings +translation_of: Web/API/PushManager/supportedContentEncodings +--- +<p>{{SeeCompatTable}}{{APIRef("Push API")}}</p> + +<p>{{domxref("PushManager")}}的只读方法 <strong><code>supportedContentEncodings</code></strong> 返回一组支持内容编码,可以用在加密信息中发送的消息。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>encodings</em>[] = PushManager.<em>supportedContentEncodings</em></pre> + +<h3 id="值">值</h3> + +<p>一个字符串数组</p> + +<h2 id="说明">说明</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">说明</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + <tr> + <td>{{SpecName('Push API','#dom-pushmanager-supportedcontentencodings','supportedContentEncodings')}}</td> + <td>{{Spec2('Push API')}}</td> + <td>最初的定义</td> + </tr> + </tbody> +</table> + +<h2 id="支持的浏览器">支持的浏览器</h2> + +<div> +<div> + + +<p>{{Compat("api.PushManager.supportedContentEncodings")}}</p> +</div> +</div> |