Notification
A notification that can be displayed to the user.
import { Notification } from '@mobrowser/api';
Example
import { Notification } from '@mobrowser/api';
const notification = new Notification({
title: 'Hello',
body: 'This is a notification.',
icon: '/path/to/icon.png'
})
notification.on('clicked', () => {
console.log('Notification clicked')
})
notification.show()
Methods
constructor()
constructor(options: NotificationOptions): void;
Creates a new notification.
| Parameter | Type | Description |
|---|---|---|
options | NotificationOptions | The options for constructing the notification. |
show()
show(): void;
Displays the notification. If the notification was previously fired, the old notification is dismissed and a new one is shown.
Example
import { Notification } from '@mobrowser/api';
const notification = new Notification({ title: 'Hello', body: 'World' })
notification.show()
dismiss()
dismiss(): void;
Programmatically dismisses the notification.
Example
import { Notification } from '@mobrowser/api';
const notification = new Notification({ title: 'Hello', body: 'World' })
notification.show()
setTimeout(() => notification.dismiss(), 3000)
Events
‘displayed’
on(event: 'displayed', listener: () => void): void;
off(event: 'displayed', listener: () => void): void;
Emitted when the notification is displayed to the user.
Example
import { Notification } from '@mobrowser/api';
const notification = new Notification({ title: 'Hello', body: 'World' })
notification.on('displayed', () => {
console.log('Notification displayed')
})
notification.show()
‘replyReceived’
on(event: 'replyReceived', listener: (reply: string) => void): void;
off(event: 'replyReceived', listener: (reply: string) => void): void;
Emitted when the user replies to the notification via the inline reply field.
Example
import { Notification } from '@mobrowser/api';
const notification = new Notification({
title: 'New message',
hasReply: true,
replyPlaceholder: 'Type a reply…'
})
notification.on('replyReceived', (reply: string) => {
console.log('User replied:', reply)
})
notification.show()
‘clicked’
on(event: 'clicked', listener: () => void): void;
off(event: 'clicked', listener: () => void): void;
Emitted when the user clicks the notification body.
Example
import { Notification } from '@mobrowser/api';
const notification = new Notification({ title: 'Hello', body: 'World' })
notification.on('clicked', () => {
console.log('Notification clicked')
})
notification.show()
‘dismissed’
on(event: 'dismissed', listener: (reason: NotificationDismissalReason) => void): void;
off(event: 'dismissed', listener: (reason: NotificationDismissalReason) => void): void;
Emitted when the notification is dismissed by the user or the system.
Example
import { Notification, NotificationDismissalReason } from '@mobrowser/api';
const notification = new Notification({ title: 'Hello', body: 'World' })
notification.on('dismissed', (reason: NotificationDismissalReason) => {
console.log('Notification dismissed:', reason)
})
notification.show()
‘failed’
on(event: 'failed', listener: (error: string) => void): void;
off(event: 'failed', listener: (error: string) => void): void;
Emitted when the notification fails to be displayed.
Example
import { Notification } from '@mobrowser/api';
const notification = new Notification({ title: 'Hello', body: 'World' })
notification.on('failed', (error: string) => {
console.error('Notification failed:', error)
})
notification.show()