本地通知
本地通知 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 | |