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
- Your page asks the user for permission.
- 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.
- You store the subscription on your server.
- To notify, your server sends an encrypted message to the endpoint, signed with your VAPID key.
- 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
tagto 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.
ब्राउज़र सपोर्ट
| फ़ीचर | Chrome / Edge desktop | Chrome Android | Safari iOS / iPadOS | Safari macOS | Firefox desktop | Firefox Android | Samsung Internet |
|---|---|---|---|---|---|---|---|
| Web Push | Chrome / Edge desktop: हाँ | Chrome Android: हाँ | Safari iOS / iPadOS: आंशिक (Home Screen web apps, iOS 16.4+) | Safari macOS: हाँ (Safari 16+ on macOS Ventura and later) | Firefox desktop: हाँ | Firefox Android: हाँ | Samsung Internet: हाँ |
| Notifications API | Chrome / Edge desktop: हाँ | Chrome Android: हाँ | Safari iOS / iPadOS: आंशिक (Home Screen web apps, iOS 16.4+) | Safari macOS: हाँ | Firefox desktop: हाँ | Firefox Android: हाँ | Samsung Internet: हाँ |
लोग अक्सर पूछते हैं
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.