@capacitor/local-notifications
本地通知 API 提供了在本地安排设备通知的方法(即无需服务器发送推送通知)。
安装
npm install @capacitor/local-notifications@latest-5
npx cap sync
Android
Android 13 需要权限检查才能发送通知。你需要相应地调用 checkPermissions() 和 requestPermissions()。
在 Android 12 及更早版本上,它不会显示提示,而会直接返回已授权状态。
从 Android 12 开始,除非在 AndroidManifest.xml 中添加以下权限,否则预定的通知将不会精确触发:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
请注意,即使存在此权限,用户仍可以从应用设置中禁用精确通知。
配置
在 Android 上,本地通知可以通过以下选项进行配置:
| 属性 | 类型 | 描述 | 自从 |
|---|---|---|---|
smallIcon | string | 设置通知的默认状态栏图标。图标应放置在应用的 res/drawable 文件夹中。此选项的值应为可绘制资源 ID,即不带扩展名的文件名。仅 Android 可用。 | 1.0.0 |
iconColor | string | 设置通知状态栏图标的默认颜色。仅 Android 可用。 | 1.0.0 |
sound | string | 设置通知的默认通知声音。在 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(...)getPending()registerActionTypes(...)cancel(...)areEnabled()getDeliveredNotifications()removeDeliveredNotifications(...)removeAllDeliveredNotifications()createChannel(...)deleteChannel(...)listChannels()checkPermissions()requestPermissions()addListener('localNotificationReceived', ...)addListener('localNotificationActionPerformed', ...)removeAllListeners()- Interfaces
- Type Aliases
- Enums
schedule(...)
schedule(options: ScheduleOptions) => Promise<ScheduleResult>
安排一个或多个本地通知。
| 参数 | 类型 |
|---|---|
options | |
返回:
Promise<ScheduleResult>
自从: 1.0.0
getPending()
getPending() => Promise<PendingResult>
获取待处理通知的列表。
返回:
Promise<PendingResult>
自从: 1.0.0
registerActionTypes(...)
registerActionTypes(options: RegisterActionTypesOptions) => Promise<void>
注册通知显示时要执行的操作。
仅 iOS 和 Android 可用。
| 参数 | 类型 |
|---|---|
options | |
自从: 1.0.0
cancel(...)
cancel(options: CancelOptions) => Promise<void>
取消待处理的通知。
| 参数 | 类型 |
|---|---|
options | |
自从: 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 | |
自从: 4.0.0
removeAllDeliveredNotifications()
removeAllDeliveredNotifications() => Promise<void>
从通知屏幕中移除所有通知。
自从: 4.0.0
createChannel(...)
createChannel(channel: Channel) => Promise<void>
创建通知频道。
仅 Android 可用。
| 参数 | 类型 |
|---|---|
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
addListener('localNotificationReceived', ...)
addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotificationSchema) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
监听通知显示的事件。
| 参数 | 类型 |
|---|---|
eventName | 'localNotificationReceived' |
listenerFunc | |
返回:
Promise<PluginListenerHandle> & PluginListenerHandle
自从: 1.0.0
addListener('localNotificationActionPerformed', ...)
addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: ActionPerformed) => void) => Promise<PluginListenerHandle> & PluginListenerHandle
监听通知上执行操作的事件。
| 参数 | 类型 |
|---|---|
eventName | 'localNotificationActionPerformed' |
listenerFunc | |
返回:
Promise<PluginListenerHandle> & PluginListenerHandle
自从: 1.0.0