API in Notifications
Web Push and PushManager.subscribe()
Published
registration.pushManager.subscribe() asks the browser’s push service for a PushSubscription
whose endpoint your server can POST encrypted messages to; the browser can deliver each message to the service
worker as a push event even when no page is open, and the worker typically shows a
notification. Together with the Notifications API this is Web Push, the only standard way for a
server to reach a PWA user who has closed the app.
Syntax
Section titled “Syntax”registration.pushManager.subscribe(options)registration.pushManager.getSubscription()registration.pushManager.permissionState(options)subscription.toJSON()subscription.unsubscribe()subscribe() resolves with a PushSubscription, or with the existing one when the registration
is already subscribed with the same options. getSubscription() resolves with the current subscription
or null. All three are available from a window and from the service worker’s own
self.registration. Support starts with Chrome 42, Firefox 44, Edge 17, Safari 16 on macOS Ventura, Safari
16.4 on iOS for Home Screen web apps (BCD api.PushManager.subscribe). Firefox 72+ requires
subscribe() to run inside a user gesture; Chromium requires applicationServerKey.
Parameters
Section titled “Parameters”| Parameter | Type | Meaning |
|---|---|---|
options.userVisibleOnly |
boolean | Promise that every push results in a visible notification. Chromium and WebKit reject false with NotAllowedError; WebKit also revokes a subscription whose push handler fails to show one (Meet Web Push, webkit.org). Firefox accepts false and applies a quota to silent pushes instead. |
options.applicationServerKey |
BufferSource or base64url string |
Your VAPID public key, an uncompressed P-256 point of 65 bytes. The server signs each send with the matching private key (RFC 8292, ietf.org). Required in Chromium; a subscription without it in Firefox cannot be migrated to VAPID later without unsubscribing. |
The returned PushSubscription serialises with toJSON() to { endpoint, expirationTime, keys: { p256dh, auth } }. endpoint is a capability URL on the browser vendor’s push service
(web.dev’s example shows an https://fcm.googleapis.com/fcm/send/… URL for Chrome, and WebKit uses push.apple.com subdomains for Safari);
it addresses the device’s subscription, so it is transmitted and stored like a credential. A subscription restricted by a VAPID key also requires a JWT signed with the matching private key (RFC 8292).
p256dh and auth are the subscription’s public key and authentication secret; the application server uses them for RFC 8291 payload
encryption, and the browser decrypts. expirationTime is null when the subscription has no expiry, as in web.dev’s example subscription.
Exceptions
Section titled “Exceptions”| Exception | When |
|---|---|
NotAllowedError DOMException |
Notification permission is "denied", or the user dismissed the prompt that subscribe() triggered; userVisibleOnly is false in Chromium or WebKit; the call is outside a user gesture in Firefox 72+; the service worker’s scope is not a secure context. |
InvalidStateError DOMException |
The registration has no active worker, or a subscription already exists with a different applicationServerKey. Unsubscribe before changing keys. |
InvalidAccessError DOMException |
applicationServerKey is not a valid P-256 public key (wrong length or not an uncompressed point). |
InvalidCharacterError DOMException |
The base64url string form of applicationServerKey fails to decode. |
AbortError DOMException |
The push service could not create the subscription, or the subscription had an error. Retry later. |
Server-side failures are HTTP statuses from the push service, not exceptions: 404 and 410
mean the subscription is gone and must be deleted; 413 means the push service judged the request body too large
(RFC 8030 says a service must not return 413 for a body of 4096 bytes or less); 429 means the
service is rate-limiting the application server.
Examples
Section titled “Examples”Subscribe from the page and receive messages in the service worker. Check for an existing subscription first.
Subscribing from a click and posting the subscription to the server
Section titled “Subscribing from a click and posting the subscription to the server”The permission request and the subscription both start in the click handler below. The key arrives from the server as a
base64url string, which the specification allows subscribe() to take directly; the
urlBase64ToUint8Array() conversion seen in older tutorials is only needed for engines that
predate that change.
button.addEventListener('click', async () => { const registration = await navigator.serviceWorker.ready; const permission = await Notification.requestPermission(); if (permission !== 'granted') return; const { publicKey } = await (await fetch('/api/push/key')).json(); const subscription = await registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: publicKey, }); await fetch('/api/push/subscriptions', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(subscription.toJSON()), });});Store the subscription keyed by user and device, and delete it when a send returns 404 or
410; a stale endpoint that keeps being pushed to is the most common cause of silent delivery
failure.
Receiving the message and resubscribing when the endpoint changes
Section titled “Receiving the message and resubscribing when the endpoint changes”The push handler should show a notification in response to each push; Safari requires one. pushsubscriptionchange fires when a subscription is refreshed, revoked, or lost outside the page’s control; the worker below resubscribes with the old subscription’s key when oldSubscription is available and tells the server.
self.addEventListener('push', (event) => { const data = event.data?.json() ?? {}; event.waitUntil( self.registration.showNotification(data.title ?? 'Update', { body: data.body ?? '', data: { url: data.url ?? '/' }, }) );});
self.addEventListener('pushsubscriptionchange', (event) => { event.waitUntil((async () => { const key = event.oldSubscription?.options.applicationServerKey; const subscription = await self.registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: key }); await fetch('/api/push/subscriptions', { method: 'POST', body: JSON.stringify(subscription.toJSON()) }); })());});pushsubscriptionchange fires in Firefox 44 (without oldSubscription, bug 1497429), Safari 16,
and Chrome 138 (BCD api.ServiceWorkerGlobalScope.pushsubscriptionchange_event); before Chrome 138
a server learns of a lost subscription from the 410 it receives, or when the page next reports its subscription.
Detecting support and reusing an existing subscription
Section titled “Detecting support and reusing an existing subscription”Check for PushManager on the registration, then use getSubscription() to read the current subscription
without changing it. The fallback is polling or in-app messages.
async function currentSubscription() { if (!('serviceWorker' in navigator) || !('PushManager' in window)) { return null; // no push: poll for updates while the app is open } const registration = await navigator.serviceWorker.getRegistration(); if (!registration?.active) return null; return registration.pushManager.getSubscription(); // existing subscription or null}A null here with permission "granted" means the user has not subscribed yet, which is the
moment to show the enable button from the first example.
Try it
Section titled “Try it”The companion demo at /demo/#push calls Notification.requestPermission() and then
pushManager.subscribe() with a VAPID public key generated for the demo, and prints the resulting
PushSubscription JSON. No server sends messages in this demo, so you can inspect the permission flow and subscription shape without receiving a push.
See also
Section titled “See also”- Push API: subscribe() method (w3.org)
- RFC 8292: Voluntary Application Server Identification (VAPID) for Web Push (ietf.org)
- Push notifications overview (web.dev)
- Notifications API
- Web Push on iOS and iPadOS
- Notification.requestPermission()
Specifications
| Specification | Status |
|---|---|
| Web Push | W3C |
- Legend
- Yes
- Partial
- Flag
- No
- Unknown
| Browser / Platform | Support | Versions | Confidence | Source | Notes |
|---|---|---|---|---|---|
| Chrome (Desktop)# | Yes | 42 | high | source | 1 |
| Chrome (Android)# | Yes | 42 | high | source | 23 |
| Edge (Desktop)# | Yes | 17 | high | source | 4 |
| Firefox (Desktop)# | Yes | 44 | high | source | 5 |
| Firefox (Android)# | Yes | 48 | high | source | 6 |
| Safari (macOS)# | Yes | 16 | high | source | 7 |
| Safari (iOS)# | Yes | 16.4 | high | source | 8 |
| Samsung Internet# | Yes | 4.0 | high | source | 910 |
| WebView (Android)# | No | — | high | source | 11 |
| Safari (iOS) · Declarative Web Push# | Yes | 18.4 | medium | source | 121314 |
- The `options` parameter with a `applicationServerKey` value is required.
- The `options` parameter with a `applicationServerKey` value is required.
- Derived by browser-compat-data mirroring from Chrome.
- The `options` parameter with a `applicationServerKey` value is required.
- From Firefox 72 onwards, can only be called in response to a user gesture such as a `click` event.
- From Firefox Android 79 onwards, can only be called in response to a user gesture such as a `click` event.
- Notifications are supported on macOS Ventura and later.
- Notifications are supported in web apps saved to the home screen.
- The `options` parameter with a `applicationServerKey` value is required.
- Derived by browser-compat-data mirroring from Chrome Android.
- No WebView Android support is recorded in browser-compat-data.
- WebKit's Safari 18.4 post: "Declarative Web Push is now available on iOS and iPadOS 18.4 for web apps added to the Home Screen." It describes Declarative Web Push as push notifications that can display without requiring a service worker.
- browser-compat-data has no Declarative Web Push entry. Its nearest key, `api.Window.pushManager`, records Safari on iOS 18.4 and is marked experimental.
- OpenPWA device test, 2026-10-20, iPhone 16 Pro, iOS 18.6.2 (22G100): `window.pushManager` is also exposed in a plain Safari tab; in a Home Screen app whose service worker is scoped to a subpath, `window.pushManager.getSubscription()` returned `null` while the worker's registration held a subscription, consistent with WebKit's statement that only a root-scoped worker shares the window's subscription.
Ecosystem & commercial policy
| Entity | Type | Context | Status | Sponsored | Notes |
|---|---|---|---|---|---|
| Apple Push (APNs)# | delivery_policy | iOS | Partial | No | iOS Web Push requires the user to add the app to the Home Screen first. |
Cite this table
Web Push compatibility data — OpenPWA · Verified 2026-10-20 · CC BY 4.0 · JSON
When reusing compatibility data, keep the attribution, identify your changes and check each row’s original source. This citation excludes market-share and reach figures, which use CC BY-SA 4.0.