@capacitor/local-notifications
Local Notifications API 提供了一种在本地安排设备通知的方法(即无需服务器发送推送通知)。
安装
npm install @capacitor/local-notifications
npx cap sync
Android
Android 13 需要权限检查才能发送通知。你需要相应地调用 checkPermissions() 和 requestPermissions()。
在 Android 12 及更早版本上,不会显示提示,将直接返回已授权。
从 Android 12 开始,除非将此权限添加到 AndroidManifest.xml,否则计划的通知不会是精确的:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
请注意,即使存在此权限,用户仍然可以从应用设置中禁用精确通知。使用 checkExactNotificationSetting() 来检查组件的值。如果用户禁用了此设置,应用将重新启动,并且任何使用精确闹钟安排的通知都将被删除。如果你的应用依赖于精确闹钟,请确保在应用启动时检查此设置(例如,在 App.appStateChange 中),以提供回退或替代行为。
在 Android 14 上,有一个名为 USE_EXACT_ALARM 的新权限。使用此权限无需向用户请求权限即可使用精确闹钟。仅当精确闹钟的使用对你的应用功能至关重要时,才应使用此权限。在此处阅读有关使用此权限的更多信息这里。
从 Android 15 开始,用户可以在私人空间中安装应用。用户可以随时锁定其私人空间,这意味着推送通知在用户解锁之前不会显示。
无法检测应用是否安装在私人空间中。因此,如果你的应用显示任何关键通知,请告知用户避免在私人空间中安装应用。
有关应用在私人空间方面行为变化的更多信息,请参考 Android 文档。
配置
本地通知可以通过以下选项进行配置:
| 属性 | 类型 | 描述 | Since |
|---|---|---|---|
smallIcon | string | 设置通知的默认状态栏图标。图标应放在应用的 res/drawable 文件夹中。此选项的值应为 drawable 资源 ID,即不带扩展名的文件名。仅适用于 Android。 | 1.0.0 |
iconColor | string | 设置通知状态栏图标的默认颜色。仅适用于 Android。 | 1.0.0 |
sound | string | 设置通知的默认通知声音。在 Android 8+ 上,设置默认渠道声音,除非应用被卸载,否则无法更改。如果未找到音频文件,Android 7.x 上将播放默认系统声音,Android 8+ 上则没有声音。仅适用于 Android。 | 1.0.0 |
presentationOptions | LocalNotificationPresentationOption[] | 这是一个可以组合的字符串数组。数组中可能的值有:- badge:更新应用图标上的角标计数(默认值)- sound:收到通知时设备会响铃/振动 - banner:通知显示为横幅 - list:通知显示在通知中心中 如果不需要任何选项,可以提供空 数组。仅适用于 iOS。 | 8.2.0 |
示例
在 capacitor.config.json 中:
{
"plugins": {
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#488AFF",
"sound": "beep.wav",
"presentationOptions": ["badge", "sound", "banner", "list"]
}
}
}
在 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",
presentationOptions: ["badge", "sound", "banner", "list"],
},
},
};
export default config;
Doze(低电耗模式)
如果设备已进入 Doze 模式,你的应用可能具有受限的功能。如果你需要在 Doze 模式下也能触发通知,请使用 allowWhileIdle: true 安排通知。谨慎使用 allowWhileIdle,因为这些通知每个应用每 9 分钟只能触发一次。
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>
安排一个或多个本地通知。
| 参数 | 类型 |
|---|---|
options | |
返回:
Promise<ScheduleResult>
Since: 1.0.0
getPending()
getPending() => Promise<PendingResult>
获取待处理通知的列表。
返回:
Promise<PendingResult>
Since: 1.0.0
registerActionTypes(...)
registerActionTypes(options: RegisterActionTypesOptions) => Promise<void>
注册在显示通知时要执行的操作。
仅适用于 iOS 和 Android。
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0
cancel(...)
cancel(options: CancelOptions) => Promise<void>
取消待处理的通知。
| 参数 | 类型 |
|---|---|
options | |
Since: 1.0.0
areEnabled()
areEnabled() => Promise<EnabledResult>
检查通知是否已启用。
返回:
Promise<EnabledResult>
Since: 1.0.0
getDeliveredNotifications()
getDeliveredNotifications() => Promise<DeliveredNotifications>
获取通知屏幕上可见的通知列表。
返回:
Promise<DeliveredNotifications>
Since: 4.0.0
removeDeliveredNotifications(...)
removeDeliveredNotifications(delivered: DeliveredNotifications) => Promise<void>
从通知屏幕中移除指定的通知。
| 参数 | 类型 |
|---|---|
delivered | |
Since: 4.0.0
removeAllDeliveredNotifications()
removeAllDeliveredNotifications() => Promise<void>
从通知屏幕中移除所有通知。
Since: 4.0.0
createChannel(...)
createChannel(channel: Channel) => Promise<void>
创建通知渠道。
仅适用于 Android。
| 参数 | 类型 |
|---|---|
channel | |
Since: 1.0.0
deleteChannel(...)
deleteChannel(args: { id: string; }) => Promise<void>
删除通知渠道。
仅适用于 Android。
| 参数 | 类型 |
|---|---|
args | { id: string; } |
Since: 1.0.0
listChannels()
listChannels() => Promise<ListChannelsResult>
获取通知渠道列表。
仅适用于 Android。
返回:
Promise<ListChannelsResult>
Since: 1.0.0
checkPermissions()
checkPermissions() => Promise<PermissionStatus>
检查显示本地通知的权限。
返回:
Promise<PermissionStatus>
Since: 1.0.0
requestPermissions()
requestPermissions() => Promise<PermissionStatus>
请求显示本地通知的权限。
返回:
Promise<PermissionStatus>
Since: 1.0.0
changeExactNotificationSetting()
changeExactNotificationSetting() => Promise<SettingsPermissionStatus>
引导用户进入应用设置屏幕以配置精确闹钟。
如果用户将设置从已授权更改为拒绝,应用 将重新启动,任何使用精确闹钟安排的通知将被删除。
在 Android < 12 上,用户不会被引导到应用设置屏幕,此函数将
返回 granted。
仅适用于 Android。
返回:
Promise<SettingsPermissionStatus>
Since: 6.0.0
checkExactNotificationSetting()
checkExactNotificationSetting() => Promise<SettingsPermissionStatus>
检查使用精确闹钟的应用设置。
仅适用于 Android。
返回:
Promise<SettingsPermissionStatus>
Since: 6.0.0
addListener('localNotificationReceived', ...)
addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotificationSchema) => void) => Promise<PluginListenerHandle>
监听通知显示时的事件。
| 参数 | 类型 |
|---|---|
eventName | 'localNotificationReceived' |
listenerFunc | |
返回:
Promise<PluginListenerHandle>
Since: 1.0.0
addListener('localNotificationActionPerformed', ...)
addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: ActionPerformed) => void) => Promise<PluginListenerHandle>
监听在通知上执行操作时的事件。
| 参数 | 类型 |
|---|---|
eventName | 'localNotificationActionPerformed' |
listenerFunc | |
返回:
Promise<PluginListenerHandle>
Since: 1.0.0
removeAllListeners()
removeAllListeners() => Promise<void>
移除此插件的所有监听器。
Since: 1.0.0
接口
ScheduleResult
| 属性 | 类型 | 描述 | Since |
|---|---|---|---|
notifications | LocalNotificationDescriptor[] | 已安排的通知列表。 | 1.0.0 |
LocalNotificationDescriptor
描述本地通知的对象。
| 属性 | 类型 | 描述 | Since |
|---|---|---|---|
id | number | 通知标识符。 | 1.0.0 |
ScheduleOptions
| 属性 | 类型 | 描述 | Since |
|---|---|---|---|
notifications | LocalNotificationSchema[] | 要安排的通知列表。 | 1.0.0 |
LocalNotificationSchema
| 属性 | 类型 | 描述 | Since |
|---|---|---|---|
title | string | 通知的标题。 | 1.0.0 |
body | string | 通知的正文,显示在标题下方。 | 1.0.0 |
largeBody | string | 设置在大文本通知样式中显示的多行文本块。 | 1.0.0 |
summaryText | string | 用于设置收件箱和大文本通知样式中的摘要文本详情。仅适用于 Android。 | 1.0.0 |
id | number | 通知标识符。在 Android 上是一个 32 位整数。因此该值应在 -2147483648 到 2147483647 之间(含边界值)。 | 1.0.0 |
schedule | | 安排此通知在稍后时间触发。 | 1.0.0 |
sound | string | 显示此通知时播放的音频文件名。在文件名中包含文件扩展名。在 iOS 上,文件应在应用 bundle 中。在 Android 上,文件应在 res/raw 文件夹中。推荐格式为 .wav,因为它同时受 iOS 和 Android 支持。仅适用于 iOS 和 Android 7.x。 对于 Android 8+,请使用配置了所需声音的渠道的 channelId。如果未找到声音文件(即空字符串或错误名称),将使用默认系统通知声音。如果未提供,Android 上将播放默认声音,iOS 上则无声音。 | 1.0.0 |
smallIcon | string | 设置自定义状态栏图标。如果设置,将覆盖 Capacitor 配置中的 smallIcon 选项。图标应放在应用的 res/drawable 文件夹中。此选项的值应为 drawable 资源 ID,即不带扩展名的文件名。仅适用于 Android。 | 1.0.0 |
largeIcon | string | 设置通知的大图标。图标应放在应用的 res/drawable 文件夹中。此选项的值应为 drawable 资源 ID,即不带扩展名的文件名。仅适用于 Android。 | 1.0.0 |
iconColor | string | 设置通知图标的颜色。仅适用于 Android。 | 1.0.0 |
attachments | Attachment[] | 设置此通知的附件。 | 1.0.0 |
actionTypeId | string | 为此通知关联一个操作类型。 | 1.0.0 |
extra | any | 设置存储在此通知中的附加数据。 | 1.0.0 |
threadIdentifier | string | 用于分组多个通知。在 UNMutableNotificationContent 上设置 threadIdentifier。仅适用于 iOS。 | 1.0.0 |
summaryArgument | string |