-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
73 lines (53 loc) · 2.09 KB
/
index.md
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
---
title: "PushManager: getSubscription() method"
short-title: getSubscription()
slug: Web/API/PushManager/getSubscription
page-type: web-api-instance-method
browser-compat: api.PushManager.getSubscription
---
{{ApiRef("Push API")}}{{SecureContext_Header}}{{AvailableInWorkers}}
The **`PushManager.getSubscription()`** method of the {{domxref("PushManager")}} interface retrieves an existing push subscription.
It returns a {{jsxref("Promise")}} that resolves to a {{domxref("PushSubscription")}} object containing details of an existing subscription. If no existing subscription exists, this resolves to a `null` value.
## Syntax
```js-nolint
getSubscription()
```
### Parameters
None.
### Return value
A {{jsxref("Promise")}} that resolves to a {{domxref("PushSubscription")}} object or `null`.
## Examples
This code snippet is taken from a [push messaging and notification sample](https://github.com/GoogleChrome/samples/tree/gh-pages/push-messaging-and-notifications). (No live demo is available.)
```js
// We need the service worker registration to check for a subscription
navigator.serviceWorker.ready.then((serviceWorkerRegistration) => {
// Do we already have a push message subscription?
serviceWorkerRegistration.pushManager
.getSubscription()
.then((subscription) => {
// Enable any UI which subscribes / unsubscribes from
// push messages.
const 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((err) => {
console.error(`Error during getSubscription(): ${err}`);
});
});
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}