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.

Returns

boolean

True if notifications are available, false otherwise.

Example

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

requestNotificationPermission()

requestNotificationPermission(): Promise<NotificationPermission>

Defined in: notifications/index.ts:27

Requests permission from the user to show notifications.

Returns

Promise<NotificationPermission>

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

Remarks

Requires HTTPS and must be triggered from a user gesture.

Example

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

showNotification()

showNotification(title, options?): Notification | undefined

Defined in: notifications/index.ts:43

Displays a browser notification if permission is granted.

Parameters

title

string

The notification title.

options?

NotificationOptions

Optional notification options.

Returns

Notification | undefined

The Notification object, or undefined if not permitted.

Example

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

notifyIfPermitted()

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

Defined in: notifications/index.ts:62

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

Parameters

title

string

The notification title.

options?

NotificationOptions

Optional notification options.

Returns

Promise<Notification | undefined>

The Notification object, or undefined if not permitted.

Example

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