@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 文档。
配置
在 Android 上,本地通知可以使用以下选项进行配置:
| 属性 | 类型 | 描述 | 起始版本 |
|---|---|---|---|
smallIcon | string | 设置通知的默认状态栏图标。图标应放置在应用的 res/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 |
示例
在 capacitor.config.json 中:
{
"plugins": {
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#488AFF",
"sound": "beep.wav"
}
}
}
在 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)模式,你的应用可能会受到功能限制。如果你需要即使在休眠模式下也能触发通知,请使用 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()- 接口
- 类型别名
- 枚举
schedule(options: ScheduleOptions) => Promise<ScheduleResult>
调度一个或多个本地通知。
| 参数 | 类型 |
|---|---|
options | |
返回值:
Promise<ScheduleResult>
自: 1.0.0
getPending()
getPending() => Promise<PendingResult>
获取待处理的通知列表。
返回值:
Promise<PendingResult>
自: 1.0.0
registerActionTypes(...)
registerActionTypes(options: RegisterActionTypesOptions) => Promise<void>
注册通知 显示时要执行的操作。
仅适用于 iOS 和 Android。
| 参数 | 类型 |
|---|---|
options | |
自: 1.0.0
cancel(...)
cancel(options: CancelOptions) => Promise<void>
取消待处理的通知。
| 参数 | 类型 |
|---|---|
options | |
自: 1.0.0
areEnabled()
areEnabled() => Promise<EnabledResult>
检查通知是否已启用。
返回值:
Promise<EnabledResult>
自: 1.0.0
getDeliveredNotifications()
getDeliveredNotifications() => Promise<DeliveredNotifications>
获取通知屏幕上可见的通知列表。
返回值:
Promise<DeliveredNotifications>
自: 4.0.0
removeDeliveredNotifications(...)
removeDeliveredNotifications(delivered: DeliveredNotifications) => Promise<void>
从通知屏幕上移除指定的通知。
| 参数 | 类型 |
|---|---|
delivered | |
自: 4.0.0
removeAllDeliveredNotifications()
removeAllDeliveredNotifications() => Promise<void>
从通知屏幕上移除所有通知。
自: 4.0.0
createChannel(...)
createChannel(channel: Channel) => Promise<void>
创建通知渠道。
仅适用于 Android。
| 参数 | 类型 |
|---|---|
channel | |
自: 1.0.0
deleteChannel(...)
deleteChannel(args: { id: string; }) => Promise<void>
删除通知渠道。
仅适用于 Android。
| 参数 | 类型 |
|---|---|
args | { id: string; } |
自: 1.0.0
listChannels()
listChannels() => Promise<ListChannelsResult>
获取通知渠道列表。
仅适用于 Android。
返回值:
Promise<ListChannelsResult>
自: 1.0.0
checkPermissions()
checkPermissions() => Promise<PermissionStatus>
检查显示本地通知的权限。
返回值:
Promise<PermissionStatus>
自: 1.0.0
requestPermissions()
requestPermissions() => Promise<PermissionStatus>
请求显示本地通知的权限。
返回值:
Promise<PermissionStatus>
自: 1.0.0
changeExactNotificationSetting()
changeExactNotificationSetting() => Promise<SettingsPermissionStatus>
引导用户进入应用设置屏幕以配置精确闹钟。
如果用户将设置从“授予”更改为“拒绝”,应用将重启,并且任何使用精确闹钟调度的通知将被删除。
在 Android < 12 上,用户将不会被引导到应用设置屏幕,而是此函数将返回 granted。
仅适用于 Android。
返回值:
Promise<SettingsPermissionStatus>
自: 6.0.0
checkExactNotificationSetting()
checkExactNotificationSetting() => Promise<SettingsPermissionStatus>
检查应用是否设置了使用精确闹钟。
仅适用于 Android。
返回值:
Promise<SettingsPermissionStatus>
自: 6.0.0
addListener('localNotificationReceived', ...)
addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotificationSchema) => void) => Promise<PluginListenerHandle>
监听通知显示事件。
| 参数 | 类型 |
|---|---|
eventName | 'localNotificationReceived' |
listenerFunc | |
返回值:
Promise<PluginListenerHandle>
自: 1.0.0
--------------------### addListener('localNotificationActionPerformed', ...)
addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: ActionPerformed) => void) => Promise<PluginListenerHandle>
监听通知上的操作被执行时的事件。
| 参数 | 类型 |
|---|---|
eventName | 'localNotificationActionPerformed' |
listenerFunc | |
返回值:
Promise<PluginListenerHandle>
自版本: 1.0.0
removeAllListeners()
removeAllListeners() => Promise<void>
移除此插件的所有监听器。
自版本: 1.0.0
接口
ScheduleResult
| 属性 | 类型 | 描述 | 自版本 |
|---|---|---|---|
notifications | LocalNotificationDescriptor[] | 已计划通知的列表。 | 1.0.0 |
LocalNotificationDescriptor
描述本地通知的对象。
| 属性 | 类型 | 描述 | 自版本 |
|---|---|---|---|
id | number | 通知标识符。 | 1.0.0 |
ScheduleOptions
| 属性 | 类型 | 描述 | 自版本 |
|---|---|---|---|
notifications | LocalNotificationSchema[] | 要计划的通知列表。 | 1.0.0 |
| -------------------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ |
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 上,文件应位于应用包中;在 Android 上,文件应位于 res/raw 文件夹中。推荐使用 .wav 格式,因为 iOS 和 Android 均支持。仅适用于 iOS 和 Android 7.x。对于 Android 8+,请使用配置了所需声音的 channelId。如果未找到声音文件(例如空字符串或错误名称),则使用默认系统通知音。如果未提供此属性,在 Android 上将产生默认声音,在 iOS 上则无声音。 | 1.0.0 |
smallIcon | string | 设置自定义状态栏图标。如果设置,将覆盖 Capacitor 配置中的 smallIcon 选项。图标应放在应用的 res/drawable 文件夹中。此选项的值应为可绘制资源 ID,即不带扩展名的文件名。仅适用于 Android。 | 1.0.0 |
largeIcon | string | 为通知设置大图标。图标应放在应用的 res/drawable 文件夹中。此选项的值应为可绘制资源 ID,即不带扩展名的文件名。仅适用于 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 | 此通知添加到分类摘要格式字符串中的字符串。在 UNMutableNotificationContent 上设置 summaryArgument。仅适用于 iOS。 | 1.0.0 |
group | string | 用于对多个通知进行分组。在 NotificationCompat.Builder 上使用提供的值调用 setGroup()。仅适用于 Android。 | 1.0.0 |
groupSummary | boolean | 如果为 true,此通知将成为一组通知的摘要。在 NotificationCompat.Builder 上使用提供的值调用 setGroupSummary()。仅在 Android 上使用 group 时可用。 | 1.0.0 |
channelId | string | 指定通知应发送到的通道。如果给定名称的通道不存在,则通知不会触发。如果未提供,将使用默认通道。在 NotificationCompat.Builder 上使用提供的值调用 setChannelId()。仅适用于 Android 8+。 | 1.0.0 |
ongoing | boolean | 如果为 true,通知将无法被滑动清除。在 NotificationCompat.Builder 上使用提供的值调用 setOngoing()。仅适用于 Android。 | 1.0.0 |
autoCancel | boolean | 如果为 true,当用户点击通知时,通知将被取消。在 NotificationCompat.Builder 上使用提供的值调用 setAutoCancel()。仅适用于 Android。 | 1.0.0 |
silent | boolean | 如果为 true,当应用处于前台时,通知将不会显示。仅适用于 iOS。 | 5.0.0 |
表示通知的计划安排。
可以使用 at、on 或 every 来安排通知。