指南
如何在Capacitor中使用富通知
使用@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- 监听按压和dismiss事件.
- 使用示例
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/