@capacitor/local-notifications
The Local Notifications API provides a way to schedule device notifications locally (i.e. without a server sending push notifications).
Install
npm install @capacitor/local-notifications
npx cap sync
Android
Android 13 requires a permission check in order to send notifications. You are required to call checkPermissions()
and requestPermissions()
accordingly.
On Android 12 and older it won't show a prompt and will just return as granted.
Starting on Android 12, scheduled notifications won't be exact unless this permission is added to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
Note that even if the permission is present, users can still disable exact notifications from the app settings.
Configuration
On Android, the Local Notifications can be configured with the following options:
Prop | Type | Description | Since |
---|---|---|---|
smallIcon | string | Set the default status bar icon for notifications. Icons should be placed in your app's res/drawable folder. The value for this option should be the drawable resource ID, which is the filename without an extension. Only available for Android. | 1.0.0 |
iconColor | string | Set the default color of status bar icons for notifications. Only available for Android. | 1.0.0 |
sound | string | Set the default notification sound for notifications. On Android 26+ it sets the default channel sound and can't be changed unless the app is uninstalled. If the audio file is not found, it will result in the default system sound being played on Android 21-25 and no sound on Android 26+. Only available for Android. | 1.0.0 |
Examples
In capacitor.config.json
:
{
"plugins": {
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#488AFF",
"sound": "beep.wav"
}
}
}
In capacitor.config.ts
:
/// <reference types="@capacitor/local-notifications" />
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
LocalNotifications: {
smallIcon: "ic_stat_icon_config_sample",
iconColor: "#488AFF",
sound: "beep.wav",
},
},
};
export default config;
Doze
If the device has entered Doze mode, your application may have restricted capabilities. If you need your notification to fire even during Doze, schedule your notification by using allowWhileIdle: true
. Make use of allowWhileIdle
judiciously, as these notifications can only fire once per 9 minutes, per app.
API
schedule(...)
getPending()
registerActionTypes(...)
cancel(...)
areEnabled()
getDeliveredNotifications()
removeDeliveredNotifications(...)
removeAllDeliveredNotifications()
createChannel(...)
deleteChannel(...)
listChannels()
checkPermissions()
requestPermissions()
addListener('localNotificationReceived', ...)
addListener('localNotificationActionPerformed', ...)
removeAllListeners()
- Interfaces
- Type Aliases
- Enums
schedule(...)
schedule(options: ScheduleOptions) => Promise<ScheduleResult>
Schedule one or more local notifications.
Param | Type |
---|---|
options | ScheduleOptions |
Returns: Promise<ScheduleResult>
Since: 1.0.0
getPending()
getPending() => Promise<PendingResult>
Get a list of pending notifications.
Returns: Promise<PendingResult>
Since: 1.0.0
registerActionTypes(...)
registerActionTypes(options: RegisterActionTypesOptions) => Promise<void>