Skip to main content

notifications

notifications is a flat object that delegates to the NotificationKit singleton. It is the most ergonomic way to use the library — import it and call methods directly.

import { notifications } from 'notification-kit';

Init and permissions

MethodSignature
init(config: NotificationConfig) => Promise<void>
requestPermission() => Promise<boolean>
checkPermission() => Promise<PermissionStatus>
isPermissionGranted() => Promise<boolean>
getPermissionState() => Promise<PermissionStatus>

Token

MethodSignature
getToken() => Promise<string>
deleteToken() => Promise<void> (throws if the provider has no deleteToken)

Topics

MethodSignature
subscribe(topic: string) => Promise<void>
unsubscribe(topic: string) => Promise<void>

Local notifications

MethodSignatureNotes
schedule(options: ScheduleOptions & LocalNotificationPayload) => Promise<void>
cancel(id: string | number) => Promise<void>
cancelAll() => Promise<void>Native only; throws on web.
getPending() => Promise<Notification[]>
getDelivered() => Promise<Notification[]>Native only; throws on web.
removeDelivered(id: string) => Promise<void>Native only.
removeAllDelivered() => Promise<void>Native only.

In-app notifications

MethodSignature
showInApp(options: InAppOptions) => Promise<string>
success(title: string, message?: string) => Promise<string>
error(title: string, message?: string) => Promise<string>
warning(title: string, message?: string) => Promise<string>
info(title: string, message?: string) => Promise<string>

Listeners

MethodSignatureNotes
onPush(cb: (notification) => void) => () => voidPush only.
onPushOpened(cb: (notification) => void) => () => voidTap / action.
on<T>(event: T, cb) => () => voidAny event.
off<T>(event: T, cb?) => void

Example

await notifications.init({ provider: 'firebase', config });

if (await notifications.requestPermission()) {
const token = await notifications.getToken();
await sendToBackend(token);
}

notifications.onPush((n) => console.log('push', n));

await notifications.schedule({
id: 42,
title: 'Reminder',
body: 'Drink water',
schedule: { every: 'hour' },
});

await notifications.success('Done');