指南
如何在 Capacitor 中使用 Rich Notifications
使用 @capgo/capacitor-rich-notifications
支持 Android 通知渠道、进度布局、操作按钮、定时发送以及 iOS 中断级别的本地通知。
安装
bun add @capgo/capacitor-rich-notifications
bunx cap sync
插件暴露的内容
checkPermission- 检查本地通知权限状态。requestPermission- 请求本地通知权限。createChannel- 创建 Android 通知渠道。createChannelGroup- 创建 Android 通知渠道组。deleteChannel- 删除 Android 通知渠道。display- 立即显示一条本地通知。schedule- 安排一条稍后显示的本地通知。cancel- 按 ID 取消单条通知。cancelAll- 取消该插件管理的所有通知。getDisplayed- 列出通知栏或通知中心里显示的通知。getPending- 列出尚未显示的定时通知。getInitialNotification- 返回打开应用的那条通知(如果有)。setBadge- 设置 iOS 应用图标的角标数量。registerActions- 注册可复用的操作类别。getPluginVersion- 返回平台实现的版本标识。addListener- 监听点击和关闭事件。
用法示例
requestPermission 以及 display
import { RichNotifications } from '@capgo/capacitor-rich-notifications';
await RichNotifications.requestPermission();
await RichNotifications.createChannel({
id: 'messages',
name: 'Messages',
importance: 'high',
});
await RichNotifications.display({
title: 'Hello',
body: 'Tap to open the app',
channelId: 'messages',
});
schedule
import { RichNotifications } from '@capgo/capacitor-rich-notifications';
await RichNotifications.schedule({
notification: {
id: 'reminder-1',
title: 'Reminder',
body: 'Your appointment starts soon',
channelId: 'messages',
},
trigger: { type: 'timestamp', timestamp: Date.now() + 60_000 },
});
完整参考
- GitHub: https://github.com/Cap-go/capacitor-rich-notifications/
- 文档:/docs/plugins/rich-notifications/