web api interface
PushManager
Secure contextAvailable in workers
The PushManager interface of the Push API provides a way to receive notifications from third-party servers as well as request URLs for push notifications.
This interface is accessed via the pushManager property.
Static properties
PushManager.supportedContentEncodings- : Returns an array of supported content codings that can be used to encrypt the payload of a push message.
Instance methods
getSubscription()- : Retrieves an existing push subscription. It returns a
Promisethat resolves to aPushSubscriptionobject containing details of an existing subscription. If no existing subscription exists, this resolves to anullvalue.
- : Retrieves an existing push subscription. It returns a
permissionState()- : Returns a
Promisethat resolves to the permission state of the currentPushManager, which will be one of'granted','denied', or'prompt'.
- : Returns a
subscribe()- : Subscribes to a push service. It returns a
Promisethat resolves to aPushSubscriptionobject containing details of a push subscription. A new push subscription is created if the current service worker does not have an existing subscription.
- : Subscribes to a push service. It returns a
Deprecated methods
hasPermission()Deprecated- : Returns a
Promisethat resolves to thePushPermissionStatusof the requesting webapp, which will be one ofgranted,denied, ordefault. Replaced bypermissionState().
- : Returns a
register()Deprecated- : Subscribes to a push subscription. Replaced by
subscribe().
- : Subscribes to a push subscription. Replaced by
registrations()Deprecated- : Retrieves existing push subscriptions. Replaced by
getSubscription().
- : Retrieves existing push subscriptions. Replaced by
unregister()Deprecated- : Unregisters and deletes a specified subscription endpoint. In the updated API, a subscription is unregistered by calling the
unsubscribe()method.
- : Unregisters and deletes a specified subscription endpoint. In the updated API, a subscription is unregistered by calling the
Example
this.onpush = (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((serviceWorkerRegistration) => {
serviceWorkerRegistration.pushManager.subscribe().then(
(pushSubscription) => {
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, the fetch() API.
},
(error) => {
console.error(error);
},
);
});
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.