blob: 1a86444b2d5f1073fc74fb05d6b0dc8cc46e62dd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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>
|