# Web Push and PushManager.subscribe()

> How PushManager.subscribe() creates a VAPID-keyed PushSubscription, what endpoint, p256dh, and auth are for, and how push events reach the service worker.

import Figure from '@components/Figure.astro';
import pushFlowDiagram from '@assets/diagrams/push-flow.svg';

`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.

<Figure src={pushFlowDiagram} alt="Sequence diagram of Web Push across the web page, service worker, push service and application server: the page requests notification permission, calls pushManager.subscribe() with the VAPID public key, receives a PushSubscription and posts it to the server. The server sends a VAPID-signed, encrypted request to the endpoint, the push service delivers it, the browser fires the push event in the service worker, which calls showNotification(). A tap fires notificationclick, and pushsubscriptionchange triggers a resubscribe." caption="Web Push from subscription to notification: who talks to whom, and which event fires where." />

## Syntax

```js
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

| 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](https://webkit.org/blog/12945/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](https://datatracker.ietf.org/doc/html/rfc8292), 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

| 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

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

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.

```js
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

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.

```js
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

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.

```js
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.

:::note
The web.dev push overview shows an example `PushSubscription` whose `endpoint` is a
`https://fcm.googleapis.com/fcm/send/...` URL and whose `expirationTime` is `null`, and explains that
the domain of the endpoint is the push service the browser uses ([Push notifications
overview](https://web.dev/articles/push-notifications-overview), web.dev). WebKit asks server
operators who restrict push endpoints to allow URLs from any subdomain of `push.apple.com`
([Meet Web Push](https://webkit.org/blog/12945/meet-web-push/), webkit.org). Chrome 63 added a
**Push** text box to the Service Workers pane in DevTools, which sends a `push` event with the typed
payload to the worker without a push server ([What's New In DevTools (Chrome
63)](https://developer.chrome.com/blog/new-in-devtools-63), developer.chrome.com).
:::

## Try it

The companion demo at [/demo/#push](/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

- [Push API: subscribe() method](https://www.w3.org/TR/push-api/#dom-pushmanager-subscribe) (w3.org)
- [RFC 8292: Voluntary Application Server Identification (VAPID) for Web Push](https://datatracker.ietf.org/doc/html/rfc8292) (ietf.org)
- [Push notifications overview](https://web.dev/articles/push-notifications-overview) (web.dev)
- [Notifications API](/reference/notifications/notifications-api/)
- [Web Push on iOS and iPadOS](/reference/notifications/ios-safari-push/)
- [Notification.requestPermission()](/reference/notifications/permissions/)