跳到主要内容
版本:v2

本地通知

本地通知 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 上,文件应位于应用 bundle 中。在 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:设置用户点击通知时自动移除通知。

LocalNotificationSchedule

属性类型
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; }

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) => 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 Object Notation (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