@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. Use checkExactNotificationSetting()
to check the the value of the setting. If a user disables this setting, the app will restart and any notification scheduled with an exact alarm will be deleted. If your application depends on exact alarms, be sure to check this setting on app launch (for example, in App.appStateChange
) in order to provide fallbacks or alternative behavior.
On Android 14, there is a new permission called USE_EXACT_ALARM
. Use this permission to use exact alarms without needing to request permission from the user. This should only be used if the use of exact alarms is central to your app's functionality. Read more about the implications of using this permission here.
From Android 15 onwards, users can install an app in the Private space. Users can lock their private space at any time, which means that push notifications are not shown until the user unlocks it.
It is not possible to detect if an app is installed in the private space. Therefore, if your app shows any critical notifications, inform your users to avoid installing the app in the private space.
For more information about the behavior changes of your app related to the private space, refer to Android documentation.
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()
changeExactNotificationSetting()
checkExactNotificationSetting()
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>