Wrapper around the Web Notifications API. Permission, show, autoClose, closeAll, showFromSW.
Module notifications | Source packages/front/fw/src/dom/utils/notifications.js | Deps none | Worker-safe partial
show(): window context only (Notificationconstructor).showFromSW(): Service Worker context (viaregistration.showNotification).
Resolve
const notifications = runtime.resolve('notifications');
// Returns: { isSupported, permission, requestPermission, show, close, closeAll, showFromSW }
API
| Method | Signature | Returns |
|---|---|---|
isSupported |
() => boolean |
typeof Notification !== 'undefined' |
permission |
() => string |
'default' / 'granted' / 'denied' |
requestPermission |
`() => Promise<'granted' | 'denied'>` |
show |
(title, options?) => handle |
Handle {close, notification} |
close |
(handle) => void |
Closes and cleans up |
closeAll |
() => void |
Closes all tracked notifications |
showFromSW |
(registration, title, options?) => Promise |
Notification via SW |
Options show()
{
// Native NotificationOptions
body: string,
icon: string,
tag: string, // replaces an existing notification with the same tag
badge: string,
image: string,
actions: [...],
// Module options
onclick: Function,
onclose: Function,
onerror: Function,
onshow: Function,
autoCloseMs: number, // automatically closes after N ms
}
Examples
Standard flow
const notifications = runtime.resolve('notifications');
// 1. Request permission (once, from a user gesture)
const perm = await notifications.requestPermission();
if (perm !== 'granted') return;
// 2. Show
const handle = notifications.show('New message', {
body: 'Alice: Hi!',
icon: '/icons/message.png',
tag: 'chat', // replaces the previous notification with this tag
autoCloseMs: 5000,
onclick: () => { window.focus(); handle.close(); },
});
// 3. Manual close if needed
handle.close();
notifications.closeAll(); // closes all module notifications
Via Service Worker
const swModule = runtime.resolve('serviceWorker');
const reg = await swModule.register('/sw.js');
await notifications.showFromSW(reg, 'Update available', {
body: 'Reload to update.',
actions: [{ action: 'reload', title: 'Reload' }],
});
Notes
show()throws ifpermission()≠'granted'. CallrequestPermission()beforehand.tag: native semantics — a new notification with the same tag replaces the old one in the notification tray.autoCloseMsis managed by the module viasetTimeout; if a manual close precedes expiration, the timer is cancelled.closeAllonly closes notifications tracked by this module (not those from other scripts or the SW).- HTTPS required except on localhost.
See also
- permissions —
permissions.query('notifications')for prior status - serviceWorker — for
showFromSW