
Introduction
Push notifications let a web application reach users even after they have closed the browser tab. News sites use them for breaking stories, e-commerce stores for price drops and order updates, and productivity tools for reminders and mentions. Thanks to the Push API, the Notifications API, and service workers, web push is now supported across Chrome, Edge, Firefox, Safari on macOS, and installed web apps on iOS. Implementing it requires a few moving parts working together securely. This guide walks you through how to implement push notifications in a web application, step by step, along with best practices that keep subscribers happy.
How AAMAX.CO Can Help You Add Push Notifications
Push notifications can drive powerful re-engagement, but only when they are implemented correctly and used thoughtfully. AAMAX.CO is a full-service digital marketing company providing web development, digital marketing, and SEO services worldwide. Their team configures service workers, VAPID keys, subscription storage, and server-side delivery, then aligns notification campaigns with broader marketing goals. Businesses looking to turn their site into an engaging, app-like experience can count on their web application development services to deliver push features that respect users and boost retention.
How Web Push Works
Web push involves four components. The browser asks the user for permission. A service worker, a background script registered by your site, receives incoming push messages and displays notifications. A push service, operated by the browser vendor, relays messages to devices. Finally, your application server stores user subscriptions and sends messages to the push service. Messages are encrypted end to end, and your server authenticates itself using VAPID (Voluntary Application Server Identification) keys.
Step 1: Serve Your Site Over HTTPS
Service workers and push only work in secure contexts. Your site must use HTTPS in production, though localhost is allowed during development. Most modern hosting platforms provide free SSL certificates automatically.
Step 2: Generate VAPID Keys
VAPID keys identify your server to push services. Generate a public and private key pair using a library such as web-push for Node.js by running npx web-push generate-vapid-keys. Store the private key securely as an environment variable on your server. The public key will be shared with the browser when subscribing.
Step 3: Register a Service Worker
Create a file such as sw.js at the root of your site so its scope covers all pages. In your main JavaScript, check for support with 'serviceWorker' in navigator and 'PushManager' in window, then register it using navigator.serviceWorker.register('/sw.js'). The service worker runs independently of your pages and can wake up when a push message arrives.
Step 4: Request Permission at the Right Moment
Timing matters. Prompting for permission the instant someone lands on your site leads to high denial rates, and browsers may suppress prompts from sites that ask too aggressively. Instead, show a custom in-page explanation first, such as "Get notified when your order ships," and only call Notification.requestPermission() after the user clicks a button. Requests must be triggered by a user gesture in many browsers, including Safari.
Step 5: Subscribe the User
Once permission is granted, subscribe through the service worker registration using registration.pushManager.subscribe() with userVisibleOnly: true and your public VAPID key converted to a Uint8Array as the applicationServerKey. The browser returns a subscription object containing an endpoint URL and encryption keys. Send this object to your backend with a POST request and store it in your database, linked to the user account if they are logged in. A user can have multiple subscriptions across devices.
Step 6: Handle Push Events in the Service Worker
Inside sw.js, listen for the push event. Parse the JSON payload, then call self.registration.showNotification() with a title and options such as body text, an icon, a badge, a URL in the data field, and optional action buttons. Wrap the call in event.waitUntil() so the browser keeps the worker alive until the notification appears. Also listen for notificationclick to close the notification and open or focus the relevant page using clients.openWindow().
Step 7: Send Push Messages From Your Server
On the server, use the web-push library or an equivalent in your language. Configure it with your VAPID details, including a contact email and both keys. To send a message, call webpush.sendNotification(subscription, JSON.stringify(payload)). Trigger sends from application events such as a new message, a shipment update, or a scheduled reminder. For large audiences, process sends through a background queue to avoid slowing down your main application.
Step 8: Clean Up Expired Subscriptions
Subscriptions can become invalid when users clear browser data, revoke permission, or uninstall the app. When the push service responds with a 404 or 410 status code, delete that subscription from your database. Regular cleanup keeps your send lists accurate and reduces wasted requests.
Supporting iOS and Safari
Safari on macOS supports standard web push. On iPhone and iPad, web push works for web apps added to the home screen, which requires a valid web app manifest with a name, icons, and a display value of standalone. Encourage iOS users to install your app and then request permission from within the installed experience.
Best Practices for Push Notifications
- Be relevant: send messages that are personal, timely, and actionable.
- Limit frequency: too many notifications lead users to block your site entirely.
- Offer preferences: let users choose which categories they receive and make unsubscribing easy.
- Write clear copy: keep titles short and bodies specific about what happened.
- Track results: measure delivery, click-through, and opt-out rates to refine your strategy.
- Respect time zones: schedule non-urgent messages for reasonable local hours.
Alternatives and Managed Services
If you prefer not to manage infrastructure, services like OneSignal, Firebase Cloud Messaging, and Pushwoosh offer dashboards, segmentation, and analytics on top of the same underlying web push standards. They speed up implementation but add a third-party dependency.
Conclusion
Implementing push notifications in a web application involves HTTPS, VAPID keys, a service worker, a well-timed permission request, subscription storage, server-side sending, and cleanup of expired subscriptions. With support now extending to iOS home screen apps, web push is a powerful channel for re-engagement. Combine solid technical implementation with respectful, relevant messaging, and push notifications will become one of the most valuable tools in your product.