Esta página aún no está disponible en Español, así que estás leyendo la versión en inglés.

Web push notifications for PWAs: a complete guide with VAPID

From permission to notification, with working code.

Capacidades y API Intermedio 3 min de lectura Revisada el 27 nov 2026

Web push lets your server send a message that the user's browser shows as a system notification, even when your site is closed. It works in Chrome, Edge, Firefox, Samsung Internet, Safari on macOS, and in Home Screen web apps on iPhone and iPad (iOS 16.4 and later).

How the pieces fit

  1. Your page asks the user for permission.
  2. The browser subscribes to its push service (run by Google, Mozilla, Apple or Microsoft) and gives you a subscription: an endpoint URL and two keys.
  3. You store the subscription on your server.
  4. To notify, your server sends an encrypted message to the endpoint, signed with your VAPID key.
  5. The push service wakes your service worker, which shows the notification.

1. Make VAPID keys (once)

VAPID keys identify your server to push services. Generate a pair with the VAPID key generator on this site (it runs in your browser), or with npx web-push generate-vapid-keys. The public key goes in your page; the private key stays on your server.

2. Ask for permission at the right moment

Never on page load. Ask after the user does something that makes notifications obviously useful ("Notify me when my order ships"), and from a click: Safari and Firefox require a user gesture, and Chrome quietly blocks sites that ask too early too often.

button.addEventListener('click', async () => {
  const permission = await Notification.requestPermission();
  if (permission === 'granted') await subscribe();
});

3. Subscribe

async function subscribe() {
  const reg = await navigator.serviceWorker.ready;
  const sub = await reg.pushManager.subscribe({
    userVisibleOnly: true,
    applicationServerKey: urlBase64ToUint8Array(VAPID_PUBLIC_KEY),
  });
  await fetch('/api/push/subscribe', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(sub) });
}
function urlBase64ToUint8Array(b64) {
  const pad = '='.repeat((4 - (b64.length % 4)) % 4);
  const raw = atob((b64 + pad).replace(/-/g, '+').replace(/_/g, '/'));
  return Uint8Array.from([...raw].map((c) => c.charCodeAt(0)));
}

4. Send from your server

Node.js with web-push:

import webpush from 'web-push';
webpush.setVapidDetails('mailto:[email protected]', VAPID_PUBLIC, VAPID_PRIVATE);
await webpush.sendNotification(subscription, JSON.stringify({ title: 'Order shipped', body: 'It arrives Thursday.', url: '/orders/123' }), { TTL: 3600, urgency: 'normal' });

PHP with web-push-php, Python with pywebpush, and libraries for Go, Java and C# work the same way. When a push service answers 404 or 410, the subscription has expired: delete it.

5. Show it in the service worker

self.addEventListener('push', (event) => {
  const data = event.data ? event.data.json() : {};
  event.waitUntil(self.registration.showNotification(data.title || 'Update', {
    body: data.body,
    icon: '/icons/icon-192.png',
    badge: '/icons/badge-72.png',
    tag: data.tag,            // replaces an older notification with the same tag
    data: { url: data.url || '/' },
  }));
});

self.addEventListener('notificationclick', (event) => {
  event.notification.close();
  const url = new URL(event.notification.data.url, self.location.origin).href;
  event.waitUntil(clients.matchAll({ type: 'window', includeUncontrolled: true }).then((wins) => {
    const open = wins.find((w) => w.url === url);
    return open ? open.focus() : clients.openWindow(url);
  }));
});

Always show a notification for every push. userVisibleOnly: true is a promise you make, and browsers may revoke permission if you break it.

Rules on iPhone and iPad

Push only works in web apps added to the Home Screen, and permission must be requested from a tap inside the installed app. See PWAs on iPhone and iPad.

Test without a server

Our notification tester asks for permission and shows a notification through this site's own service worker, so you can see how notifications look on your device.

Good practice

  • Let people choose topics and frequency, and turn them off in your app.
  • Use tag to replace rather than pile up notifications.
  • Keep payloads small (under about 3 KB) and fetch details when the notification is clicked.
  • Respect quiet hours in the user's time zone.

Compatibilidad de navegadores

Compatibilidad de los navegadores con funciones de PWA
FunciónChrome / Edge desktopChrome AndroidSafari iOS / iPadOSSafari macOSFirefox desktopFirefox AndroidSamsung Internet
Web Push Chrome / Edge desktop: Sí Chrome Android: Sí Safari iOS / iPadOS: Parcial (Home Screen web apps, iOS 16.4+) Safari macOS: Sí (Safari 16+ on macOS Ventura and later) Firefox desktop: Sí Firefox Android: Sí Samsung Internet: Sí
Notifications API Chrome / Edge desktop: Sí Chrome Android: Sí Safari iOS / iPadOS: Parcial (Home Screen web apps, iOS 16.4+) Safari macOS: Sí Firefox desktop: Sí Firefox Android: Sí Samsung Internet: Sí

Ver la tabla completa

Preguntas frecuentes

Do I need Firebase Cloud Messaging for web push?

No. Standard Web Push with VAPID keys works with every browser's push service directly. FCM is one option, not a requirement.

Why does my permission prompt not appear?

Most browsers require the request to come from a user gesture, and Chrome may show a quieter prompt for sites that are often blocked. On iOS the app must be opened from the Home Screen.