本地通知
Local Notification API 提供了一种调度“本地”通知的方式——这些通知在设备上被调度和传递,与从服务器发送的“推送”通知不同。
本地通知非常适合提醒用户自上次访问应用以来发生的变化,提供提醒功能,以及在应用不在前台时传递离线信息。
schedule(...)getPending()registerActionTypes(...)cancel(...)areEnabled()createChannel(...)deleteChannel(...)listChannels()requestPermission()addListener(...)addListener(...)removeAllListeners()- 接口
示例
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 | |
areEnabled()
areEnabled() => Promise<LocalNotificationEnabledResult>
返回值:
Promise<LocalNotificationEnabledResult>
createChannel(...)
createChannel(channel: NotificationChannel) => Promise<void>
| 参数 | 类型 |
|---|---|
channel | |
deleteChannel(...)
deleteChannel(channel: NotificationChannel) => Promise<void>
| 参数 | 类型 |
|---|---|
channel | |
listChannels()
listChannels() => Promise<NotificationChannelList>
返回值:
Promise<NotificationChannelList>
requestPermission()
requestPermission() => Promise<NotificationPermissionResponse>
返回值:
Promise<NotificationPermissionResponse>
addListener(...)
addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotification) => void) => PluginListenerHandle
| 参数 | 类型 |
|---|---|
eventName | "localNotificationReceived" |
listenerFunc | |
返回值:
PluginListenerHandle
addListener(...)
addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: LocalNotificationActionPerformed) => void) => PluginListenerHandle
| 参数 | 类型 |
|---|---|
eventName | "localNotificationActionPerformed" |
listenerFunc | |
返回值:
PluginListenerHandle
removeAllListeners()
removeAllListeners() => void
移除此插件的所有原生监听器
接口
LocalNotificationScheduleResult#### LocalNotification
| 属性 | 类型 | 说明 |
|---|---|---|
title | string | 通知标题 |
body | string | 通知正文 |
id | number | 通知唯一标识符 |
schedule | | 通知触发时间安排 |
sound | string | 带 扩展名的音频文件名。在 iOS 上,文件应位于应用程序包中。在 Android 上,文件应位于 res/raw 文件夹中。不适用于 Android 26+ 版本(Android O 及更新版本),建议使用 .wav 格式,因为该格式受到两个平台的支持。 |
smallIcon | string | 仅限 Android:设置自定义状态栏图标。如果设置,将覆盖 capacitor.config.json 中的默认图标 |
iconColor | string | 仅限 Android:设置通知图标的颜色 |
attachments | LocalNotificationAttachment[] | 通知附件 |
actionTypeId | string | 操作类型标识符 |
extra | any | 额外数据 |
threadIdentifier | string | 仅限 iOS:设置用于通知分组的线程标识符 |
summaryArgument | string | 仅限 iOS 12+:设置用于通知分组的摘要参数 |
group | string | 仅限 Android:设置用于通知分组的组标识符,类似于 iOS 上的 threadIdentifier。 |
groupSummary | boolean | 仅限 Android:将此通知指定为组的摘要(应与 group 属性一起使用)。 |
channelId | string | 仅限 Android:设置将生成本地通知的通知渠道。如果具有给定名称的渠道不存在,则通知不会触发。如果未提供,将使用默认渠道。 |
ongoing | boolean | 仅限 Android:设置通知为持续状态。如果设置为 true,通知无法被滑动清除。 |
autoCancel | boolean | 仅限 Android:设置通知在用户点击时自动移除 |
| 属性 | 类型 |
|---|---|
at | |
repeats | boolean |
every | "year" | "month" | "two-weeks" | "week" | "day" | "hour" | "minute" | "second" |
count | number |
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
| 属性 | 类型 |
|---|---|
id | string |
url | string |
options | |
LocalNotificationAttachmentOptions
| 属性 | 类型 |
|---|---|
iosUNNotificationAttachmentOptionsTypeHintKey | string |
iosUNNotificationAttachmentOptionsThumbnailHiddenKey | string |
iosUNNotificationAttachmentOptionsThumbnailClippingRectKey | string |
iosUNNotificationAttachmentOptionsThumbnailTimeKey | string |
LocalNotificationPendingList
| 属性 | 类型 |
|---|---|
notifications | LocalNotificationRequest[] |
LocalNotificationRequest
| 属性 | 类型 |
|---|---|
id | string |
LocalNotificationActionType
| 属性 | 类型 |
|---|---|
id | string |
actions | LocalNotificationAction[] |
iosHiddenPreviewsBodyPlaceholder | string |
iosCustomDismissAction | boolean |
iosAllowInCarPlay | boolean |
iosHiddenPreviewsShowTitle | boolean |
iosHiddenPreviewsShowSubtitle | boolean |
LocalNotificationAction
| 属性 | 类型 |
|---|---|
id | string |
title | string |
requiresAuthentication | boolean |
foreground | boolean |
destructive | boolean |
input | boolean |
inputButtonTitle | string |
inputPlaceholder | string |
LocalNotificationEnabledResult
| 属性 | 类型 | 描述 |
|---|---|---|
value | boolean | 设备是否启用了本地通知 |
NotificationChannel
| 属性 | 类型 |
|---|---|
id | string |
name | string |
description | string |
sound | string |
importance | 1 | 2 | 5 | 4 | 3 |
visibility | 0 | 1 | -1 |
lights | boolean |
lightColor | string |
vibration | boolean |
NotificationChannelList
| 属性 | 类型 |
|---|---|
channels | NotificationChannel[] |
NotificationPermissionResponse
| 属性 | 类型 |
|---|---|
granted | boolean |
PluginListenerHandle
| 属性 | 类型 |
|---|---|
remove | () => void |
LocalNotificationActionPerformed
| 属性 | 类型 |
|---|---|
actionId | string |
inputValue | string |
notification | |