The service worker is the small JavaScript file that runs in the background of the user's browser. It is the component that actually receives push notifications when the application's tab is closed, and that decides how to display them.
How It's Installed
The platform ships a default service worker at /sw.js. On every page load the application registers it if it isn't already installed. Browsers cache the file aggressively; you bump its embedded version string to force an update.
Customising Notifications
The service worker calls a hook function before showing each notification. You can override:
- Title: Default is the channel name; override to show the sender or context.
- Body: The main message. Keep under 200 characters — long bodies get truncated by the OS.
- Icon and badge: Use a 192×192 PNG for the icon and a monochrome PNG for the badge.
- Action buttons: Up to two quick actions (e.g. “Mark as read”, “Reply”). Each is wired to a click handler in the service worker.
Worked Examples
- Chat notification: Title is the sender's name, body is the message text, icon is the sender's avatar, action buttons are “Reply” (opens chat thread) and “Mark as read”.
- Booking reminder: Title is “Appointment in 30 minutes”, body lists location and patient, action button is “View booking”.
- Order update: Title is the order number, body is the new status, badge shows the platform logo.
Note: Service workers can only be served from HTTPS origins. They are scoped to the path they were registered from — the default /sw.js has root scope and controls every page on the origin.