notifications
Request permission and show desktop notifications via the Notifications API.
Import: @rtorcato/browser-common/notifications
๐ MDN: Notifications API ยท ๐ caniuse: notifications
Needs a secure context and user permission (requestNotificationPermission).
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
Notification | โ 20+ [1,2] | โ 14+ | โ 22+ | โ 7+ | โ partial 16.4+ [3,4] | Limited |
Notification.requestPermission | โ 20+ | โ 14+ | โ 22+ [5,6] | โ 15+ | โ partial 16.4+ [7] | Limited |
- Chrome: Starting in Chrome 49, notifications do not work in incognito mode.
- Chrome: Before Chrome 42, service worker additions were not supported.
- iOS Safari: The Notification interface is undefined, unless the page is a web app saved to the home screen. The app's manifest must have a non-default display value.
- iOS Safari: A notification can only be sent from a service worker. To show a notification, see ServiceWorkerRegistration.showNotification().
- Firefox: From Firefox 70 onwards, cannot be called from a cross-origin iframe.
- Firefox: From Firefox 72 onwards, can only be called in response to a user gesture such as a click event.
- iOS Safari: The parent Notification interface is undefined unless the page is a web app saved to the home screen. The app's manifest must have a non-default display value.
Needs: secure context (HTTPS), a user gesture
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Notification ยท Notification.requestPermission_static
Exampleโ
import { isNotificationAvailable, notifyIfPermitted } from '@rtorcato/browser-common/notifications'
button.addEventListener('click', async () => {
if (isNotificationAvailable()) {
await notifyIfPermitted('Done', { body: 'Upload complete' })
}
})
Exportsโ
isNotificationAvailable()โ feature checkrequestNotificationPermission()โ asks the user for notification permissionshowNotification(title, options?)โ shows a notification if permission is already grantednotifyIfPermitted(title, options?)โ requests permission if needed, then shows a notification
See the API reference for full signatures.