aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/pushmanager/subscribe/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/pushmanager/subscribe/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/pushmanager/subscribe/index.html')
-rw-r--r--files/zh-cn/web/api/pushmanager/subscribe/index.html138
1 files changed, 138 insertions, 0 deletions
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>