跳到主要内容
版本:v2

本地通知

Local Notification API 提供了一种调度“本地”通知的方式——这些通知在设备上被调度和传递,与从服务器发送的“推送”通知不同。

本地通知非常适合提醒用户自上次访问应用以来发生的变化,提供提醒功能,以及在应用不在前台时传递离线信息。

示例

import { Plugins } from '@capacitor/core';
const { LocalNotifications } = Plugins;

const notifs = await LocalNotifications.schedule({
notifications: [
{
title: '标题',
body: '正文',
id: 1,
schedule: { at: new Date(Date.now() + 1000 * 5) },
sound: null,
attachments: null,
actionTypeId: '',
extra: null,
},
],
});
console.log('已调度的通知', notifs);

本地通知配置(仅适用于 Android)

本地通知插件允许在 capacitor.config.json 中的 Android 平台配置项中添加以下配置值:

  • smallIcon: 允许您设置本地通知的默认图标。
  • iconColor: 允许您设置本地通知图标的默认颜色。
  • sound: 允许您设置默认的通知提示音。在 Android 26+ 上,它设置的是默认通道的提示音,并且除非卸载应用,否则无法更改。
 "plugins": {
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#488AFF",
"sound": "beep.wav"
}
}

API

schedule(...)

schedule(options: { notifications: LocalNotification[]; }) => Promise<LocalNotificationScheduleResult>
参数类型
options{ notifications: LocalNotification[]; }

返回值:

Promise<LocalNotificationScheduleResult>


getPending()

getPending() => Promise<LocalNotificationPendingList>

返回值:

Promise<LocalNotificationPendingList>


registerActionTypes(...)

registerActionTypes(options: { types: LocalNotificationActionType[]; }) => Promise<void>
参数类型
options{ types: LocalNotificationActionType[]; }

cancel(...)

cancel(pending: LocalNotificationPendingList) => Promise<void>
参数类型
pending
LocalNotificationPendingList

areEnabled()

areEnabled() => Promise<LocalNotificationEnabledResult>

返回值:

Promise<LocalNotificationEnabledResult>


createChannel(...)

createChannel(channel: NotificationChannel) => Promise<void>
参数类型
channel
NotificationChannel

deleteChannel(...)

deleteChannel(channel: NotificationChannel) => Promise<void>
参数类型
channel
NotificationChannel

listChannels()

listChannels() => Promise<NotificationChannelList>

返回值:

Promise<NotificationChannelList>


requestPermission()

requestPermission() => Promise<NotificationPermissionResponse>

返回值:

Promise<NotificationPermissionResponse>


addListener(...)

addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotification) => void) => PluginListenerHandle
参数类型
eventName"localNotificationReceived"
listenerFunc
(notification: LocalNotification) => void

返回值:

PluginListenerHandle


addListener(...)

addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: LocalNotificationActionPerformed) => void) => PluginListenerHandle
参数类型
eventName"localNotificationActionPerformed"
listenerFunc
(notificationAction: LocalNotificationActionPerformed) => void

返回值:

PluginListenerHandle


removeAllListeners()

removeAllListeners() => void

移除此插件的所有原生监听器


接口

LocalNotificationScheduleResult#### LocalNotification

属性类型说明
titlestring通知标题
bodystring通知正文
idnumber通知唯一标识符
schedule
LocalNotificationSchedule
通知触发时间安排
soundstring带扩展名的音频文件名。在 iOS 上,文件应位于应用程序包中。在 Android 上,文件应位于 res/raw 文件夹中。不适用于 Android 26+ 版本(Android O 及更新版本),建议使用 .wav 格式,因为该格式受到两个平台的支持。
smallIconstring仅限 Android:设置自定义状态栏图标。如果设置,将覆盖 capacitor.config.json 中的默认图标
iconColorstring仅限 Android:设置通知图标的颜色
attachmentsLocalNotificationAttachment[]通知附件
actionTypeIdstring操作类型标识符
extraany额外数据
threadIdentifierstring仅限 iOS:设置用于通知分组的线程标识符
summaryArgumentstring仅限 iOS 12+:设置用于通知分组的摘要参数
groupstring仅限 Android:设置用于通知分组的组标识符,类似于 iOS 上的 threadIdentifier。
groupSummaryboolean仅限 Android:将此通知指定为组的摘要(应与 group 属性一起使用)。
channelIdstring仅限 Android:设置将生成本地通知的通知渠道。如果具有给定名称的渠道不存在,则通知不会触发。如果未提供,将使用默认渠道。
ongoingboolean仅限 Android:设置通知为持续状态。如果设置为 true,通知无法被滑动清除。
autoCancelboolean仅限 Android:设置通知在用户点击时自动移除
属性类型
at
Date
repeatsboolean
every"year" | "month" | "two-weeks" | "week" | "day" | "hour" | "minute" | "second"
countnumber
on{ year?: number; month?: number; day?: number; hour?: number; minute?: number; }

提供日期和时间的基本存储与检索功能。| 方法 | 签名 | 描述 | | ---------------------- | --------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | | 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) => number | 使用本地时间设置 Date 对象中的秒值。 | | setUTCSeconds | (sec: number, ms?: number) => number | 使用协调世界时(UTC)设置 Date 对象中的秒值。 || setMinutes | (min: number, sec?: number, ms?: number) => number | 使用本地时间设置 Date 对象中的分钟值。 | | setUTCMinutes | (min: number, sec?: number, ms?: number) => number | 使用协调世界时 (UTC) 设置 Date 对象中的分钟值。 | | setHours | (hours: number, min?: number, sec?: number, ms?: number) => number | 使用本地时间设置 Date 对象中的小时值。 | | setUTCHours | (hours: number, min?: number, sec?: number, ms?: number) => number | 使用协调世界时 (UTC) 设置 Date 对象中的小时值。 | | setDate | (date: number) => number | 使用本地时间设置 Date 对象中的月份日期值(数值)。 | | setUTCDate | (date: number) => number | 使用协调世界时 (UTC) 设置 Date 对象中的月份日期值(数值)。 | | setMonth | (month: number, date?: number) => number | 使用本地时间设置 Date 对象中的月份值。 | | setUTCMonth | (month: number, date?: number) => number | 使用协调世界时 (UTC) 设置 Date 对象中的月份值。 | | setFullYear | (year: number, month?: number, date?: number) => number | 使用本地时间设置 Date 对象中的年份值。 | | setUTCFullYear | (year: number, month?: number, date?: number) => number | 使用协调世界时 (UTC) 设置 Date 对象中的年份值。 | | toUTCString | () => string | 返回一个使用协调世界时 (UTC) 转换为字符串的日期。 | | toISOString | () => string | 以 ISO 格式返回日期的字符串值。 | | toJSON | (key?: any) => string | 由 JSON.stringify 方法使用,以便在 JavaScript 对象表示法 (JSON) 序列化过程中转换对象的数据。 |#### LocalNotificationAttachment

属性类型
idstring
urlstring
options
LocalNotificationAttachmentOptions

LocalNotificationAttachmentOptions

属性类型
iosUNNotificationAttachmentOptionsTypeHintKeystring
iosUNNotificationAttachmentOptionsThumbnailHiddenKeystring
iosUNNotificationAttachmentOptionsThumbnailClippingRectKeystring
iosUNNotificationAttachmentOptionsThumbnailTimeKeystring

LocalNotificationPendingList

属性类型
notificationsLocalNotificationRequest[]

LocalNotificationRequest

属性类型
idstring

LocalNotificationActionType

属性类型
idstring
actionsLocalNotificationAction[]
iosHiddenPreviewsBodyPlaceholderstring
iosCustomDismissActionboolean
iosAllowInCarPlayboolean
iosHiddenPreviewsShowTitleboolean
iosHiddenPreviewsShowSubtitleboolean

LocalNotificationAction

属性类型
idstring
titlestring
requiresAuthenticationboolean
foregroundboolean
destructiveboolean
inputboolean
inputButtonTitlestring
inputPlaceholderstring

LocalNotificationEnabledResult

属性类型描述
valueboolean设备是否启用了本地通知

NotificationChannel

属性类型
idstring
namestring
descriptionstring
soundstring
importance1 | 2 | 5 | 4 | 3
visibility0 | 1 | -1
lightsboolean
lightColorstring
vibrationboolean

NotificationChannelList

属性类型
channelsNotificationChannel[]

NotificationPermissionResponse

属性类型
grantedboolean

PluginListenerHandle

属性类型
remove() => void

LocalNotificationActionPerformed

属性类型
actionIdstring
inputValuestring
notification
LocalNotification