diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/pushmanager/getsubscription | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/pushmanager/getsubscription')
-rw-r--r-- | files/zh-cn/web/api/pushmanager/getsubscription/index.html | 95 |
1 files changed, 95 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> |