Local Notifications
The Local Notification API provides a way to schedule "local" notifications - notifications that are scheduled and delivered on the device as opposed to "push" notifications sent from a server.
Local Notifications are great for reminding the user about a change in the app since they last visited, providing reminder features, and delivering offline information without the app being in the foreground.
schedule(...)
getPending()
registerActionTypes(...)
cancel(...)
areEnabled()
createChannel(...)
deleteChannel(...)
listChannels()
requestPermission()
addListener(...)
addListener(...)
removeAllListeners()
- Interfaces
Example
import { Plugins } from '@capacitor/core';
const { LocalNotifications } = Plugins;
const notifs = await LocalNotifications.schedule({
notifications: [
{
title: 'Title',
body: 'Body',
id: 1,
schedule: { at: new Date(Date.now() + 1000 * 5) },
sound: null,
attachments: null,
actionTypeId: '',
extra: null,
},
],
});
console.log('scheduled notifications', notifs);
Local Notifications configuration (Android only)
The local notification plugin allows the following configuration values to be added in capacitor.config.json
for the Android platform:
smallIcon
: It allows you to set the default icon for the local notification.iconColor
: It allows you to set the default color for the local notification icon.sound
: It allows you to set the default notification sound. On Android 26+ it sets the default channel sound and can't be changed unless the app is uninstalled.
"plugins": {
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#488AFF",
"sound": "beep.wav"
}
}
API
schedule(...)
schedule(options: { notifications: LocalNotification[]; }) => Promise<LocalNotificationScheduleResult>
Param | Type |
---|---|
options | { notifications: LocalNotification[]; } |
Returns:
Promise<LocalNotificationScheduleResult>
getPending()
getPending() => Promise<LocalNotificationPendingList>
Returns:
Promise<LocalNotificationPendingList>
registerActionTypes(...)
registerActionTypes(options: { types: LocalNotificationActionType[]; }) => Promise<void>
Param | Type |
---|---|
options | { types: LocalNotificationActionType[]; } |
cancel(...)
cancel(pending: LocalNotificationPendingList) => Promise<void>
Param | Type |
---|---|
pending |
|
areEnabled()
areEnabled() => Promise<LocalNotificationEnabledResult>
Returns:
Promise<LocalNotificationEnabledResult>