NotificationsPort
NotificationsPort displays and manages OS-level browser notifications, including rich notifications with buttons and progress indicators.
Context
- Availability: Background
- Source:
packages/ports/src/background/notifications/notifications.port.ts
Key Methods
create(options: WebExtNotificationOptions): Promise<string>- Show a notification, returns its ID.create(notificationId: string, options: WebExtNotificationOptions): Promise<string>- Show a notification with an explicit ID.clear(notificationId: string): Promise<boolean>- Dismiss a notification.getAll(): Promise<{ [notificationId: string]: boolean }>- List all active notifications.onClickedAddListener(listener: (notificationId: string) => void): void- Subscribe to notification click events.onClickedRemoveListener(listener: (notificationId: string) => void): void- Unsubscribe from click events.onClosedAddListener(listener: (notificationId: string, byUser: boolean) => void): void- Subscribe to notification close events.
Usage
import { NotificationsPort } from '@hexajs-dev/ports';
import { Injectable, HexaContext } from '@hexajs-dev/common';
@Injectable({ context: HexaContext.Background })
export class DownloadNotificationService {
constructor(private readonly notifications: NotificationsPort) {}
async notifyDownloadComplete(filename: string) {
const id = await this.notifications.create({
type: 'basic',
iconUrl: 'icons/icon-48.png',
title: 'Download complete',
message: `${filename} is ready.`,
});
this.notifications.onClickedAddListener((notificationId) => {
if (notificationId === id) {
this.notifications.clear(id);
}
});
}
}
API Reference Appendix
Below is the exhaustive, auto-generated technical reference for all types, interfaces, and signatures associated with this API.
Classes
NotificationsPort
import { NotificationsPort } from '@hexajs-dev/ports';
class NotificationsPort { ... }
Methods
clear()
clear(notificationId: string): Promise<boolean>
create()
create(options: HexaWebNotificationOptions): Promise<string>
create()
create(notificationId: string, options: HexaWebNotificationOptions): Promise<string>
create()
create(notificationIdOrOptions: string | HexaWebNotificationOptions, optionsMaybe?: HexaWebNotificationOptions): Promise<string>
getAll()
getAll(): Promise<{
[notificationId: string]: boolean;
}>
onButtonClickedAddListener()
onButtonClickedAddListener(listener: (notificationId: string, buttonIndex: number) => void): void
onButtonClickedRemoveListener()
onButtonClickedRemoveListener(listener: (notificationId: string, buttonIndex: number) => void): void
onClickedAddListener()
onClickedAddListener(listener: (notificationId: string) => void): void
onClickedRemoveListener()
onClickedRemoveListener(listener: (notificationId: string) => void): void
onClosedAddListener()
onClosedAddListener(listener: (notificationId: string, byUser: boolean) => void): void
onClosedRemoveListener()
onClosedRemoveListener(listener: (notificationId: string, byUser: boolean) => void): void