fn
useWebNotification
v0.0.14testeddemoReactive, SSR-safe wrapper around the Web Notification API with
permission handling and onClick/onShow/onError/onClose event hooks.
Examples
ts
const { show, isSupported, onClick } = useWebNotification({
title: 'Hello',
body: 'You have a new message',
icon: '/icon.png',
});
onClick(() => console.log('clicked'));
if (isSupported.value)
show();ts
// Override options per call
const { show } = useWebNotification();
show({ title: 'Override', body: 'Per-call body' });Demo
Loading demo…
Signature
ts
export function useWebNotification(
options: UseWebNotificationOptions ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
options? | UseWebNotificationOptions | Default notification options plus behavior flags |
Returns
UseWebNotificationReturnisSupported, notification, permissionGranted, show, close, ensurePermissionGranted, and the onClick/onShow/onError/onClose hooks| Property | Type | Description |
|---|---|---|
isSupported | ComputedRef<boolean> | Whether the Notification API is supported in the current environment. |
notification | ShallowRef<Notification | null> | The currently displayed Notification, or null when none is shown. |
permissionGranted | ShallowRef<boolean> | Whether notification permission has been granted. |
show | (overrides?: WebNotificationOptions) => Promise<Notification | undefined> | Display a notification, optionally overriding the default options for this
call. Resolves with the created Notification, or undefined when
unsupported or permission is not granted. |
close | () => void | Close the currently displayed notification (if any). |
ensurePermissionGranted | () => Promise<boolean | undefined> | Request permission if it has not yet been granted (and was not denied).
Resolves with the resulting granted state, or undefined when unsupported. |
onClick | EventHookOn<Event> | Register a listener fired when the notification is clicked. |
onShow | EventHookOn<Event> | Register a listener fired when the notification is shown. |
onError | EventHookOn<Event> | Register a listener fired when the notification errors. |
onClose | EventHookOn<Event> | Register a listener fired when the notification is closed. |