PWA Installable and Push Notifications Deep Dive
Make PWAs installable with a web app manifest and enable push notifications with the Push API.
What you'll learn
- Web App Manifest: icons, display modes, start_url
- Install prompt and beforeinstallprompt event
- Push API and notification subscription
- Notification display and action handling
Concept
PWA Installable and Push Notifications Deep Dive
PWAs can be installed to the home screen and send push notifications, making them feel like native apps.
Web App Manifest
The manifest is a JSON file linked from HTML. It defines name, icons (192px+512px), start_url, display (standalone), theme_color. Installability requires manifest + SW + HTTPS.
Install Prompt
The browser shows an install prompt when criteria are met. Intercept beforeinstallprompt, save it, show custom button. Call promptEvent.prompt() to show dialog.
Push API
- Request Notification permission. 2. Subscribe via PushManager.subscribe() with VAPID keys. 3. Server sends push to endpoint. 4. SW receives push event and shows notification.
Notification Handling
In SW push event, call showNotification(title, options). Options: body, icon, actions, tag. notificationclick event handles clicks, typically opening a URL.
Example
const manifest = {name:'My App', short_name:'MyApp', icons:[{src:'/icon-192.png',sizes:'192x192'},{src:'/icon-512.png',sizes:'512x512'}], start_url:'/', display:'standalone', theme_color:'#3b82f6'};
console.log('Manifest:', JSON.stringify(manifest, null, 2));
let deferred = null;
window.addEventListener('beforeinstallprompt', e => { e.preventDefault(); deferred = e; console.log('Install ready'); });
async function installApp() { if (!deferred) return; deferred.prompt(); const {outcome} = await deferred.userChoice; console.log('Install:', outcome); deferred = null; }
self.addEventListener('push', e => { const d = e.data ? e.data.json() : {}; e.waitUntil(self.registration.showNotification(d.title||'Notification', {body: d.body||''})); });
console.log('PWA install and push defined');
The manifest defines app metadata. beforeinstallprompt is captured for custom install button. SW push event shows notifications.
Try it
- JSON Formatter
Manifest and push payloads are JSON - format and validate.
Common mistakes
The mistake
Not providing 512px icon in the manifest.
The fix
Installability requires both 192px and 512px icons. Missing either prevents the install prompt.
The mistake
Sending push without userVisibleOnly: true.
The fix
Push must show a notification. Silent push is not allowed in browsers.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.