Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 37 ยท Push Notifications

Push Notifications: The Big Picture

Understand how web push works: the Push API, Notifications API, push services, VAPID keys, subscriptions, permissions and platform support, step by step.

Push notifications let a server reach a user when the app is not open: a new message, a delivery update, a reminder. On the web they are built from two separate APIs that people often mix up, and from a third party you do not control. Understanding who does what makes everything in the next lessons easier.

This lesson is the map. You will meet the actors, follow one message from your server to the screen, learn what VAPID keys are for, and see the rules that browsers and operating systems apply.

Two APIs, one feature

  • The Push API delivers a message to your service worker. It is invisible: it wakes the worker and gives it data.
  • The Notifications API shows something on the screen. It can be called from the page or, more often, from the service worker.

They are independent. You can show a notification without any push (the page calls new Notification() or registration.showNotification()), and you can receive a push without showing anything (to refresh a cache). Browsers require that a push which wakes the worker normally results in a visible notification: Chrome and Safari allow only a few silent pushes before the subscription is penalized, so "userVisibleOnly" is part of the subscribe call.

The actors

flowchart LR
  A["Your app server"] -->|"1. POST encrypted message<br/>(signed with VAPID key)"| P["Push service<br/>(run by the browser vendor)"]
  P -->|"2. delivers when device online"| B["Browser"]
  B -->|"3. wakes"| W["Your service worker<br/>push event"]
  W -->|"4. showNotification()"| N["Notification on screen"]

Your server never talks to the device directly. It talks to the push service the browser chose.

The push service is operated by the browser or platform vendor (Google's FCM for Chrome, Mozilla's autopush for Firefox, Apple's service for Safari, Microsoft's for Edge). You do not pick it. Each subscription contains an endpoint, a long URL that points to a specific browser on a specific device at that service. Anyone who has the endpoint can try to send to it, which is why the next part exists.

VAPID: proving who you are

VAPID (Voluntary Application Server Identification) uses a key pair that belongs to your server:

  • The public key is given to the browser when subscribing. The subscription is bound to it.
  • The private key stays on your server. You sign each push request with it.

The push service checks the signature against the public key stored in the subscription. A stranger who found the endpoint cannot send messages, and the vendor can contact you (through the subject claim, usually a mailto: address) if your server misbehaves. You generate the key pair once and keep it for the life of the app. Changing it invalidates every existing subscription.

The subscription object

After the user agrees, pushManager.subscribe() returns a subscription. Converted with toJSON() it looks like this (values shortened):

JSON
{
  "endpoint": "https://fcm.googleapis.com/fcm/send/dX3...",
  "expirationTime": null,
  "keys": {
    "p256dh": "BNc...",
    "auth": "k8J..."
  }
}

p256dh and auth are the browser's public encryption key and a secret. Your server uses them to encrypt the payload, so even the push service cannot read the message. Your server stores one record like this per user per device.

The lifecycle at a glance

JS Push flow, step by step

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last line is important in real life: a 404 or 410 Gone from the push service means the subscription is dead, and your server must delete it from the database.

Permission and user trust

Notifications need explicit permission, and browsers punish sites that ask carelessly. Three states exist: default (not asked), granted and denied. Once denied, your code cannot ask again; only the user can change it in browser settings.

Good practice:

  • Ask after the user did something that shows value, with a button they press, never on first load.
  • Explain what they will get ("Tell me when my order ships") before triggering the browser prompt.
  • Make sure there is a way to turn notifications off inside the app.

Browsers increasingly hide or auto-deny prompts from sites that users reject often, so spending the one chance you have wisely matters. The next lessons show the code: subscribing to push, sending push messages and designing notifications.

JS The permission state machine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Platform support

Platform Web push
Chrome, Edge, Firefox, Opera (desktop) Yes
Chrome and Firefox on Android Yes
Safari on macOS (16+) Yes
Safari on iOS and iPadOS (16.4+) Yes, only for a PWA added to the Home Screen
Other iOS browsers Same rule: only installed web apps

The iOS rule shapes your design: the permission prompt must come from a user gesture inside the installed app, so an install flow usually precedes the notification flow. Requirements shared by every platform: a secure context (HTTPS), a registered service worker, and a manifest on iOS.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the Push API do on its own?

    The Push API wakes the worker with data. Displaying something is the Notifications API's job.
  2. 2. What is the VAPID private key used for?

    The server signs requests with the private key; the push service verifies against the public key in the subscription.
  3. 3. A push service answers 410 Gone. What should the server do?

    410 and 404 mean the subscription no longer exists. Keeping it only wastes requests.
  4. 4. On iPhone, when does web push work?

    Apple supports web push for installed web apps on iOS and iPadOS 16.4 and later.

Summary

  • Push (delivery to the worker) and Notifications (display) are separate APIs.
  • Your server sends to the browser vendor's push service, signed with a VAPID private key; payloads are encrypted for the specific subscription.
  • Store one subscription per device and delete it on 404/410.
  • Ask for permission at a meaningful moment; a denied answer is final until the user changes settings.
  • iOS requires an installed web app.

Next: Subscribing to Push