This example demonstrates how to handle browser push notifications using vanilla HTML & JavaScript via Web Push API. WebPush API sends messages from the server (backend) to the client (browser/mobile phone). The API uses HTTP2 protocol which requires HTTPS connection (unless you're in localhost) The server sends messages to the client's browser via Firebase Cloud Messaging API (or FCM for short).
- You need to obtain the public VAPID key to be able to connect to the server. Look up generate-VAPID-key.md file.
- Make sure Backend server uses the same VAPID keys.
- The index.html sets up the application template and connects to the scripts.js file
- The scripts file registers a browser service-worker (via service-worker.js file in the root directory). This file also calls specified API endpoints in response to button clicks on UI
- The service-worker.js files handles notifications.
service-worker.js
📁 public
|__ index.html
|__ scripts.js
- Run your backend server
- Run the frontend server using the Live-Server extension for VSCode (http://127.0.0.1:5500/public/index.html)