Skip to content

Lesson 8 of 9 · 28m

Web Push Architecture, Permissions, and Notification UX

Implement push as a server-signed delivery system with expiring subscriptions, careful permission timing, payload hygiene, and actionable notification design.

Push is where PWAs stop feeling like documents and start feeling like applications. It is also where teams collect subscriptions, lose track of expiry, leak private content into lock screens, and train users to click "Block" forever. Web Push is not a websocket replacement and not a guaranteed alarm system. It is a best-effort messaging channel mediated by browser and platform push services.

Understand the moving parts

The browser subscribes through the service worker. The subscription contains an endpoint and keys. Your application server stores it and later sends an encrypted Web Push message to the endpoint:

app server -> push service endpoint -> browser -> service worker -> notification

VAPID identifies your application server to the push service. Generate a key pair once per environment with npx web-push generate-vapid-keys. The public key goes to the browser; the private key stays server-side and signs push requests.

Subscribe after permission is earned

const reg = await navigator.serviceWorker.ready;
const permission = await Notification.requestPermission();
if (permission !== 'granted') return;
const subscription = await reg.pushManager.subscribe({
  userVisibleOnly: true,
  applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY),
});
await fetch('/api/push/subscriptions', {
  method: 'POST',
  body: JSON.stringify({ subscription }),
});

userVisibleOnly: true means every push must eventually show a visible notification on Chromium. Silent background processing is not what this API grants. Ask after enabling a feature with obvious push value, not during initial page load. Users need a reason to trust you before the browser prompt appears.

Notifications are interfaces, not banners

Handle push in the worker:

self.addEventListener('push', (event) => {
  const data = event.data?.json() ?? {};
  event.waitUntil(self.registration.showNotification(data.title ?? 'Fieldnotes', {
    body: data.body ?? 'You have a new update.',
    icon: '/icons/icon-192.png',
    tag: data.tag,
    data: { url: data.url ?? '/' },
  }));
});
self.addEventListener('notificationclick', (event) => {
  event.notification.close();
  event.waitUntil(clients.openWindow(event.notification.data.url));
});

Use tag to collapse repeated updates for the same conversation or object. Every notification should answer what happened, why it matters, and where tapping will take the user.

Do not put sensitive payloads on lock screens

Push payloads may be visible on locked devices and pass through platform infrastructure. Push a wake-up or minimal summary, then fetch details after authentication. Even "You were mentioned in Operations" can be sensitive in some products; if necessary, make the notification generic and require opening the app for context.

Subscriptions expire

Store endpoint, expiration metadata when available, creation time, user agent, and last successful send. Handle pushsubscriptionchange where supported. Treat server-side 404/410 responses as dead subscriptions and delete them. Measure delivery failure rates; an old subscription table can be mostly a graveyard.

Field notes

  • VAPID keys are environment-specific. Do not reuse production keys in demos or CI.
  • iOS push support is tied to installed web apps and platform versions, not merely having a service worker in a tab.
  • Notification clicks can focus an existing client instead of opening another window; use clients.matchAll() for better UX.
  • Test battery optimization and background restrictions on real Android hardware.
  • Permission state is part of product UX, not just a boolean API detail.

Was this lesson helpful?

Sign in to rate