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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
---
title: PushManager
slug: Web/API/PushManager
tags:
- API
- Experimental
- Interface
- NeedsTranslation
- Push
- Push API
- Reference
- Service Workers
- TopicStub
- WebAPI
translation_of: Web/API/PushManager
---
<p>{{SeeCompatTable}}{{ApiRef("Push API")}}</p>
<p>The <code>PushManager</code> interface of the <a href="/en-US/docs/Web/API/Push_API">Push API</a> provides a way to receive notifications from third-party servers as well as request URLs for push notifications.</p>
<p>This interface is accessed via the {{domxref("ServiceWorkerRegistration.pushManager")}} property.</p>
<div class="note">
<p><strong>Note</strong>: This interface replaces functionality previously offered by the obsolete {{domxref("PushRegistrationManager")}} interface.</p>
</div>
<h2 id="Properties">Properties</h2>
<p><em>None.</em></p>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{domxref("PushManager.getSubscription()")}}</dt>
<dd>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 <code>null</code> value.</dd>
<dt>{{domxref("PushManager.permissionState()")}}</dt>
<dd>Returns a {{jsxref("Promise")}} that resolves to the permission state of the current {{domxref("PushManager")}}, which will be one of <code>'granted'</code>, <code>'denied'</code>, or <code>'default'</code>.</dd>
<dt>{{domxref("PushManager.subscribe()")}}</dt>
<dd>Subscribes to a push service. It returns a {{jsxref("Promise")}} that resolves to a {{domxref("PushSubscription")}} object containing details of a push subscription. A new push subscription is created if the current service worker does not have an existing subscription.</dd>
</dl>
<h3 id="Deprecated_methods">Deprecated methods</h3>
<dl>
<dt>{{domxref("PushManager.hasPermission()")}} {{deprecated_inline}}</dt>
<dd>Returns a {{jsxref("Promise")}} that resolves to the <code>PushPermissionStatus</code> of the requesting webapp, which will be one of <code>granted</code>, <code>denied</code>, or <code>default</code>. Replaced by {{domxref("PushManager.permissionState()")}}.</dd>
<dt>{{domxref("PushManager.register()")}} {{deprecated_inline}}</dt>
<dd>Subscribes to a push subscription. Replaced by {{domxref("PushManager.subscribe()")}}.</dd>
<dt>{{domxref("PushManager.registrations()")}} {{deprecated_inline}}</dt>
<dd>Retrieves existing push subscriptions. Replaced by {{domxref("PushManager.getSubscription()")}}.</dd>
<dt>{{domxref("PushManager.unregister()")}} {{deprecated_inline}}</dt>
<dd>Unregisters and deletes a specified subscription endpoint. In the updated API, a subscription is unregistered by calling the {{domxref("PushSubscription.unsubscribe()")}} method.</dd>
</dl>
<h2 id="Example">Example</h2>
<pre class="brush: js">this.onpush = function(event) {
console.log(event.data);
// From here we can write the data to IndexedDB, send it to any open
// windows, display a notification, etc.
}
navigator.serviceWorker.register('serviceworker.js').then(
function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe().then(
function(pushSubscription) {
console.log(pushSubscription.subscriptionId);
console.log(pushSubscription.endpoint);
// The push subscription details needed by the application
// server are now available, and can be sent to it using,
// for example, an XMLHttpRequest.
}, function(error) {
// During development it often helps to log errors to the
// console. In a production environment it might make sense to
// also report information about errors back to the
// application server.
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>
{{Compat("api.PushManager")}}
<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>
|