跳到主要内容
版本:v6

@capacitor/local-notifications

Local Notifications API 提供在本地调度设备通知的方式(即无需服务器发送推送通知)。

安装

npm install @capacitor/local-notifications@latest-6
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 的新权限。使用此权限可以在无需请求用户权限的情况下使用精确闹钟。仅当精确闹钟的使用对应用功能至关重要时才应使用此权限。在此阅读有关使用此权限的影响的更多信息 here

从 Android 15 开始,用户可以在私人空间中安装应用。用户可以随时锁定其私人空间,这意味着推送通知在用户解锁之前不会显示。

无法检测应用是否安装在私人空间中。因此,如果您的应用显示任何关键通知,请告知您的用户避免在私人空间中安装该应用。

有关您的应用与私人空间相关的行为变更的更多信息,请参阅 Android 文档

配置

在 Android 上,可以使用以下选项配置本地通知:

属性类型描述始于
smallIconstring设置通知的默认状态栏图标。图标应放在应用的 res/drawable 文件夹中。此选项的值应为 drawable 资源 ID,即不带扩展名的文件名。仅适用于 Android。1.0.0
iconColorstring设置通知状态栏图标的默认颜色。仅适用于 Android。1.0.0
soundstring设置通知的默认通知声音。在 Android 26+ 上,它设置默认频道声音,除非卸载应用否则无法更改。如果未找到音频文件,则在 Android 21-25 上将播放默认系统声音,在 Android 26+ 上则无声。仅适用于 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 模式

如果设备已进入 Doze 模式,您的应用可能具有受限的能力。如果您需要通知即使在 Doze 模式下也能触发,请通过使用 allowWhileIdle: true 来调度您的通知。请明智地使用 allowWhileIdle,因为这些通知每应用每 9 分钟只能触发一次

API

schedule(...)

schedule(options: ScheduleOptions) => Promise<ScheduleResult>

调度一个或多个本地通知。

参数类型
options
ScheduleOptions

返回:

Promise<ScheduleResult>

始于: 1.0.0


getPending()

getPending() => Promise<PendingResult>

获取待处理通知的列表。

返回:

Promise<PendingResult>

始于: 1.0.0


registerActionTypes(...)

registerActionTypes(options: RegisterActionTypesOptions) => Promise<void>

注册通知显示时要执行的操作。

仅适用于 iOS 和 Android。

参数类型
options
RegisterActionTypesOptions

始于: 1.0.0


cancel(...)

cancel(options: CancelOptions) => Promise<void>

取消待处理的通知。

参数类型
options
CancelOptions

始于: 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
DeliveredNotifications

始于: 4.0.0


removeAllDeliveredNotifications()

removeAllDeliveredNotifications() => Promise<void>

从通知屏幕中移除所有通知。

始于: 4.0.0


createChannel(...)

createChannel(channel: Channel) => Promise<void>

创建一个通知频道。

仅适用于 Android。

参数类型
channel
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
(notification: LocalNotificationSchema) => void

返回:

Promise<PluginListenerHandle>

始于: 1.0.0


addListener('localNotificationActionPerformed', ...)

addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: ActionPerformed) => void) => Promise<PluginListenerHandle>

监听通知上执行操作时的事件。

参数类型
eventName'localNotificationActionPerformed'
listenerFunc
(notificationAction: ActionPerformed) => void

返回:

Promise<PluginListenerHandle>

始于: 1.0.0


removeAllListeners()

removeAllListeners() => Promise<void>

移除此插件的所有监听器。

始于: 1.0.0


接口

ScheduleResult

属性类型描述始于
notificationsLocalNotificationDescriptor[]已调度的通知列表。1.0.0

LocalNotificationDescriptor

描述本地通知的对象。

属性类型描述始于
idnumber通知标识符。1.0.0

ScheduleOptions

属性类型描述始于
notificationsLocalNotificationSchema[]要调度的通知列表。1.0.0

LocalNotificationSchema

属性类型描述始于
titlestring通知的标题。1.0.0
bodystring通知的正文,显示在标题下方。1.0.0
largeBodystring设置用于大文本通知样式的多行文本块。1.0.0
summaryTextstring用于在收件箱和大文本通知样式中设置摘要文本详情。仅适用于 Android。1.0.0
idnumber通知标识符。在 Android 上是一个 32 位整数。因此该值应在 -2147483648 到 2147483647 之间(含)。1.0.0
schedule
Schedule
将此通知调度到稍后时间。1.0.0
soundstring显示此通知时要播放的音频文件名。文件名中包含文件扩展名。在 iOS 上,文件应位于应用包中。在 Android 上,文件应位于 res/raw 文件夹中。推荐使用 .wav 格式,因为它同时被 iOS 和 Android 支持。仅适用于 iOS 和 Android < 26。对于 Android 26+,使用已配置所需声音的 channelId。如果未找到声音文件(即空字符串或错误名称),将使用系统默认通知声音。如果未提供,在 Android 上会产生默认声音,在 iOS 上则无声。1.0.0
smallIconstring设置自定义状态栏图标。如果设置,将覆盖 Capacitor 配置中的 smallIcon 选项。图标应放在应用的 res/drawable 文件夹中。此选项的值应为 drawable 资源 ID,即不带扩展名的文件名。仅适用于 Android。1.0.0
largeIconstring设置通知的大图标。图标应放在应用的 res/drawable 文件夹中。此选项的值应为 drawable 资源 ID,即不带扩展名的文件名。仅适用于 Android。1.0.0
iconColorstring设置通知图标的颜色。仅适用于 Android。1.0.0
attachmentsAttachment[]设置此通知的附件。1.0.0
actionTypeIdstring为此通知关联一个操作类型。1.0.0
extraany设置要存储在此通知中的额外数据。1.0.0
threadIdentifierstring用于对多个通知进行分组。在 UNMutableNotificationContent 上设置 threadIdentifier。仅适用于 iOS。1.0.0
summaryArgumentstring此通知添加到类别摘要格式字符串的字符串。在 UNMutableNotificationContent 上设置 summaryArgument。仅适用于 iOS。1.0.0
groupstring用于对多个通知进行分组。使用提供的值在 NotificationCompat.Builder 上调用 setGroup()。仅适用于 Android。1.0.0
groupSummaryboolean如果为 true,此通知将成为通知组的摘要。使用提供的值在 NotificationCompat.Builder 上调用 setGroupSummary()。仅在使用 group 时适用于 Android。1.0.0
channelIdstring指定通知应发送到的频道。如果具有给定名称的频道不存在,则通知不会触发。如果未提供,将使用默认频道。使用提供的值在 NotificationCompat.Builder 上调用 setChannelId()。仅适用于 Android 26+。1.0.0
ongoingboolean如果为 true,通知不能被滑动清除。使用提供的值在 NotificationCompat.Builder 上调用 setOngoing()。仅适用于 Android。1.0.0
autoCancelboolean如果为 true,用户点击通知时通知被取消。使用提供的值在 NotificationCompat.Builder 上调用 setAutoCancel()。仅适用于 Android。1.0.0
inboxListstring[]设置用于收件箱样式通知显示的字符串列表。最多允许 5 个字符串。仅适用于 Android。1.0.0
silentboolean如果为 true,应用在前台时通知将不会出现。仅适用于 iOS。5.0.0

Schedule

表示通知的调度计划。

使用 atonevery 之一来调度通知。

属性类型描述始于
at
Date
在特定的日期和时间调度通知。1.0.0
repeatsbooleanat 指定的日期和时间重复投递此通知。仅适用于 iOS 和 Android。1.0.0
allowWhileIdleboolean允许此通知在 Doze 模式下触发。仅适用于 Android 23+。请注意这些通知每应用每 9 分钟只能触发一次1.0.0
on
ScheduleOn
在特定间隔上调度通知。类似于调度 cron 任务。仅适用于 iOS 和 Android。1.0.0
every
ScheduleEvery
在特定间隔调度通知。1.0.0
countnumber限制通知在 every 指定的间隔内投递的次数。1.0.0

Date

启用日期和时间的存储和检索。

方法签名描述
toString() => string返回日期的字符串表示。字符串格式取决于区域设置。
toDateString() => string以字符串值返回日期。
toTimeString() => string以字符串值返回时间。
toLocaleString() => string以适合主机环境当前区域设置的值返回字符串。
toLocaleDateString() => string以适合主机环境当前区域设置的字符串值返回日期。
toLocaleTimeString() => string以适合主机环境当前区域设置的字符串值返回时间。
valueOf() => number返回自 1970 年 1 月 1 日 UTC 午夜以来的存储时间值(毫秒)。
getTime() => number获取时间值(毫秒)。
getFullYear() => number使用本地时间获取年份。
getUTCFullYear() => number使用协调世界时(UTC)获取年份。
getMonth() => number使用本地时间获取月份。
getUTCMonth() => number使用协调世界时(UTC)获取 Date 对象的月份。
getDate() => number使用本地时间获取月份中的日期。
getUTCDate() => number使用协调世界时(UTC)获取月份中的日期。
getDay() => number使用本地时间获取星期几。
getUTCDay() => number使用协调世界时(UTC)获取星期几。
getHours() => number使用本地时间获取日期的小时数。
getUTCHours() => number使用协调世界时(UTC)获取 Date 对象的小时值。
getMinutes() => number使用本地时间获取 Date 对象的分钟数。
getUTCMinutes() => number使用协调世界时(UTC)获取 Date 对象的分钟数。
getSeconds() => number使用本地时间获取 Date 对象的秒数。
getUTCSeconds() => number使用协调世界时(UTC)获取 Date 对象的秒数。
getMilliseconds() => number使用本地时间获取 Date 的毫秒数。
getUTCMilliseconds() => number使用协调世界时(UTC)获取 Date 对象的毫秒数。
getTimezoneOffset() => number获取本地计算机时间和协调世界时(UTC)之间的分钟差。
setTime(time: number) => number设置 Date 对象中的日期和时间值。
setMilliseconds(ms: number) => number使用本地时间设置 Date 对象中的毫秒值。
setUTCMilliseconds(ms: number) => number使用协调世界时(UTC)设置 Date 对象中的毫秒值。
setSeconds(sec: number, ms?: number | undefined) => number使用本地时间设置 Date 对象中的秒值。
setUTCSeconds(sec: number, ms?: number | undefined) => number使用协调世界时(UTC)设置 Date 对象中的秒值。
setMinutes(min: number, sec?: number | undefined, ms?: number | undefined) => number使用本地时间设置 Date 对象中的分钟值。
setUTCMinutes(min: number, sec?: number | undefined, ms?: number | undefined) => number使用协调世界时(UTC)设置 Date 对象中的分钟值。
setHours(hours: number, min?: number | undefined, sec?: number | undefined, ms?: number | undefined) => number使用本地时间设置 Date 对象中的小时值。
setUTCHours(hours: number, min?: number | undefined, sec?: number | undefined, ms?: number | undefined) => number使用协调世界时(UTC)设置 Date 对象中的小时值。
setDate(date: number) => number使用本地时间设置 Date 对象中的月份日期值。
setUTCDate(date: number) => number使用协调世界时(UTC)设置 Date 对象中的月份日期值。
setMonth(month: number, date?: number | undefined) => number使用本地时间设置 Date 对象中的月份值。
setUTCMonth(month: number, date?: number | undefined) => number使用协调世界时(UTC)设置 Date 对象中的月份值。
setFullYear(year: number, month?: number | undefined, date?: number | undefined) => number使用本地时间设置 Date 对象的年份。
setUTCFullYear(year: number, month?: number | undefined, date?: number | undefined) => number使用协调世界时(UTC)设置 Date 对象中的年份值。
toUTCString() => string使用协调世界时(UTC)将日期转换为字符串。
toISOString() => string以 ISO 格式的字符串值返回日期。
toJSON(key?: any) => string由 JSON.stringify 方法使用,以实现对象数据用于 JSON 序列化的转换。

ScheduleOn

属性类型
yearnumber
monthnumber
daynumber
weekday
Weekday
hournumber
minutenumber
secondnumber

Attachment

表示通知的附件。

属性类型描述始于
idstring附件标识符。1.0.0
urlstring附件的 URL。使用 res scheme 引用 Web 资源,例如 res:///assets/img/icon.png。也接受 file URL。1.0.0
options
AttachmentOptions
附件选项。1.0.0

AttachmentOptions

属性类型描述始于
iosUNNotificationAttachmentOptionsTypeHintKeystringUNNotificationAttachment 的可哈希选项中设置 UNNotificationAttachmentOptionsTypeHintKey 键。仅适用于 iOS。1.0.0
iosUNNotificationAttachmentOptionsThumbnailHiddenKeystringUNNotificationAttachment 的可哈希选项中设置 UNNotificationAttachmentOptionsThumbnailHiddenKey 键。仅适用于 iOS。1.0.0
iosUNNotificationAttachmentOptionsThumbnailClippingRectKeystringUNNotificationAttachment 的可哈希选项中设置 UNNotificationAttachmentOptionsThumbnailClippingRectKey 键。仅适用于 iOS。1.0.0
iosUNNotificationAttachmentOptionsThumbnailTimeKeystringUNNotificationAttachment 的可哈希选项中设置 UNNotificationAttachmentOptionsThumbnailTimeKey 键。仅适用于 iOS。1.0.0

PendingResult

属性类型描述始于
notificationsPendingLocalNotificationSchema[]待处理通知的列表。1.0.0

PendingLocalNotificationSchema

属性类型描述始于
titlestring通知的标题。1.0.0
bodystring通知的正文,显示在标题下方。1.0.0
idnumber通知标识符。1.0.0
schedule
Schedule
将此通知调度到稍后时间。1.0.0
extraany设置要存储在此通知中的额外数据。1.0.0

RegisterActionTypesOptions

属性类型描述始于
typesActionType[]要注册的操作类型列表。1.0.0

ActionType

操作的集合。

属性类型描述始于
idstring操作类型的 ID。在通知中由 actionTypeId 键引用。1.0.0
actionsAction[]与此操作类型关联的操作列表。1.0.0
iosHiddenPreviewsBodyPlaceholderstring设置 UNNotificationCategoryhiddenPreviewsBodyPlaceholder。仅适用于 iOS。1.0.0
iosCustomDismissActionbooleanUNNotificationCategory 的选项中设置 customDismissAction。仅适用于 iOS。1.0.0
iosAllowInCarPlaybooleanUNNotificationCategory 的选项中设置 allowInCarPlay。仅适用于 iOS。1.0.0
iosHiddenPreviewsShowTitlebooleanUNNotificationCategory 的选项中设置 hiddenPreviewsShowTitle。仅适用于 iOS。1.0.0
iosHiddenPreviewsShowSubtitlebooleanUNNotificationCategory 的选项中设置 hiddenPreviewsShowSubtitle。仅适用于 iOS。1.0.0

Action

通知显示时可以执行的操作。

属性类型描述始于
idstring操作标识符。在 'actionPerformed' 事件中作为 actionId 引用。1.0.0
titlestring为此操作显示的标题文本。1.0.0
requiresAuthenticationbooleanUNNotificationAction 的选项中设置 authenticationRequired。仅适用于 iOS。1.0.0
foregroundbooleanUNNotificationAction 的选项中设置 foreground。仅适用于 iOS。1.0.0
destructivebooleanUNNotificationAction 的选项中设置 destructive。仅适用于 iOS。1.0.0
inputboolean使用 UNTextInputNotificationAction 而不是 UNNotificationAction。仅适用于 iOS。1.0.0
inputButtonTitlestringUNTextInputNotificationAction 上设置 textInputButtonTitle。仅在 inputtrue 时适用于 iOS。1.0.0
inputPlaceholderstringUNTextInputNotificationAction 上设置 textInputPlaceholder。仅在 inputtrue 时适用于 iOS。1.0.0

CancelOptions

属性类型描述始于
notificationsLocalNotificationDescriptor[]要取消的通知列表。1.0.0

EnabledResult

属性类型描述始于
valueboolean设备是否已启用本地通知。1.0.0

DeliveredNotifications

属性类型描述始于
notificationsDeliveredNotificationSchema[]通知屏幕上可见的通知列表。1.0.0

DeliveredNotificationSchema

属性类型描述始于
idnumber通知标识符。4.0.0
tagstring通知标签。仅适用于 Android。4.0.0
titlestring通知的标题。4.0.0
bodystring通知的正文,显示在标题下方。4.0.0
groupstring通知的配置分组。仅适用于 Android。4.0.0
groupSummaryboolean此通知是否为通知组的摘要。仅适用于 Android。4.0.0
dataany包含在通知负载中的任何附加数据。仅适用于 Android。4.0.0
extraany要存储在此通知中的额外数据。仅适用于 iOS。4.0.0
attachmentsAttachment[]此通知的附件。仅适用于 iOS。1.0.0
actionTypeIdstring与此通知关联的操作类型。仅适用于 iOS。4.0.0
schedule
Schedule
用于触发此通知的调度计划。仅适用于 iOS。4.0.0
soundstring通知显示时使用的声音。仅适用于 iOS。4.0.0

Channel

属性类型描述默认值始于
idstring频道标识符。1.0.0
namestring此频道的人类友好名称(对用户显示)。1.0.0
descriptionstring此频道的描述(对用户显示)。1.0.0
soundstring投递到此频道的通知应播放的声音。重要性至少为 3 的通知频道应有声音。声音文件的文件名应相对于 Android 应用的 res/raw 目录指定。如果未提供声音或未找到声音文件,则不会使用声音。1.0.0
importance
Importance
投递到此频道的通知的中断级别。
3
1.0.0
visibility
Visibility
投递到此频道的通知的可见性。此设置用于控制投递到此频道的通知是否在锁定屏幕上显示,以及如果显示,是否以编辑形式显示。1.0.0
lightsboolean投递到此频道的通知是否应显示通知指示灯(在支持的设备上)。1.0.0
lightColorstring投递到此频道的通知的指示灯颜色。仅在此频道上启用了指示灯且设备支持时有效。支持的颜色格式为 #RRGGBB#RRGGBBAA1.0.0
vibrationboolean投递到此频道的通知是否应振动。1.0.0

ListChannelsResult

属性类型描述始于
channelsChannel[]通知频道列表。1.0.0

PermissionStatus

属性类型描述始于
display
PermissionState
显示通知的权限状态。1.0.0

SettingsPermissionStatus

属性类型描述始于
exact_alarm
PermissionState
使用精确闹钟的权限状态。6.0.0

PluginListenerHandle

属性类型
remove() => Promise<void>

ActionPerformed

属性类型描述始于
actionIdstring执行的操作的标识符。1.0.0
inputValuestring用户在通知上输入的值。仅适用于 iOS 上 input 设置为 true 的通知。1.0.0
notification
LocalNotificationSchema
原始通知架构。1.0.0

类型别名

ScheduleEvery

'year' | 'month' | 'two-weeks' | 'week' | 'day' | 'hour' | 'minute' | 'second'

Importance

重要性级别。有关更多详细信息,请参阅 Android 开发者文档

1 | 2 | 3 | 4 | 5

Visibility

通知可见性。有关更多详细信息,请参阅 Android 开发者文档

-1 | 0 | 1

PermissionState

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'

枚举

Weekday

成员
Sunday1
Monday2
Tuesday3
Wednesday4
Thursday5
Friday6
Saturday7