Skip to main content

@rtorcato/browser-common / notifications

notifications

Functions​

isNotificationAvailable()​

isNotificationAvailable(): boolean

Defined in: notifications/index.ts:10

Checks if the Notifications API is available in the current browser.

Example​

import { isNotificationAvailable } from '@rtorcato/browser-common/notifications'
if (isNotificationAvailable()) askPermission()

Returns​

boolean

True if notifications are available, false otherwise.


requestNotificationPermission()​

requestNotificationPermission(): Promise<NotificationPermission>

Defined in: notifications/index.ts:27

Requests permission from the user to show notifications.

Example​

import { requestNotificationPermission } from '@rtorcato/browser-common/notifications'
button.addEventListener('click', async () => {
if ((await requestNotificationPermission()) === 'granted') notify()
})

Returns​

Promise<NotificationPermission>

The permission result ('granted', 'denied', or 'default').

Remarks​

Requires HTTPS and must be triggered from a user gesture.


showNotification()​

showNotification(title, options?): Notification | undefined

Defined in: notifications/index.ts:43

Displays a browser notification if permission is granted.

Example​

import { showNotification } from '@rtorcato/browser-common/notifications'
showNotification('Done', { body: 'Upload complete' })

Parameters​

title​

string

The notification title.

options?​

NotificationOptions

Optional notification options.

Returns​

Notification | undefined

The Notification object, or undefined if not permitted.


notifyIfPermitted()​

notifyIfPermitted(title, options?): Promise<Notification | undefined>

Defined in: notifications/index.ts:62

Requests permission if needed, then shows a notification if possible.

Example​

import { notifyIfPermitted } from '@rtorcato/browser-common/notifications'
await notifyIfPermitted('Hello', { body: 'World' })

Parameters​

title​

string

The notification title.

options?​

NotificationOptions

Optional notification options.

Returns​

Promise<Notification | undefined>

The Notification object, or undefined if not permitted.