指南
如何在Capacitor中使用通知
使用@capgo/capacitor-notifications
从Capgo发送本机iOS和Android推送通知,支持用户查找、徽章、前台/打开跟踪、后台回调和静默Capgo实时更新检查。
该插件是为Capgo通知管道而构建的。Capgo在Postgres中存储平台凭证元数据和推送控制数据,而活跃设备状态和交付事件则写入Cloudflare分析引擎。
安装
使用 Capgo CLI 进行引导式设置:
npx @capgo/cli@latest notifications setup com.example.app
手动安装:
npm install @capgo/capacitor-notifications @capgo/capacitor-updater
npx cap sync
配置:
import { CapgoNotifications } from '@capgo/capacitor-notifications'
await CapgoNotifications.configure({
appId: 'com.example.app',
autoUpdater: true,
updateInstallMode: 'next',
})
注册已签名用户:
mint identityProof 从您的后端生成一个签名,然后在您的登录成功后注册设备。 POST /notifications/recipients/proof监听事件:
await CapgoNotifications.register({
externalId: 'customer-user-123',
identityProof,
tags: ['paid'],
attributes: { plan: 'team' },
consent: true,
})
iOS 设置:
await CapgoNotifications.addListener('notificationReceived', (notification) => {
console.log('Received', notification)
})
await CapgoNotifications.addListener('notificationOpened', (event) => {
console.log('Opened', event.notification.id)
})
await CapgoNotifications.addListener('backgroundNotification', async (event) => {
try {
console.log('Background payload', event.notification.data)
} finally {
await event.finish()
}
})
启用:
推送通知 页面/区域: Capgo 营销网站。角色: 短 UI 标签或导航项。消息键 `push_notifications` (推送通知)。 并且 背景模式 > 远程通知 在 Xcode 中。
将远程通知从 ios/App/App/AppDelegate.swift:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
NotificationCenter.default.post(name: Notification.Name("CapgoNotificationsRemoteNotification"), object: userInfo)
completionHandler(.newData)
}
Android 设置
运行 npx cap sync android在 Capgo 中配置 Android 平台凭据,请求 Android 13+ 的通知权限,并在具有 Google Play 服务的设备或模拟器上测试。
调试清单
- 运行
npx @capgo/cli@latest notifications setup com.example.app从包含capacitor.config.*. - 在 Capgo 中通过外部客户 ID搜索接收者。
- 确认设备具有平台
android或ios并且已获得许可。 - 添加临时监听器
registrationChanged,notificationReceived,notificationOpened,并backgroundNotification. - 从控制台发送测试通知。
- 查看排队、发送、接收和打开事件的统计数据。
- 对于静默更新检查,验证
@capgo/capacitor-updater已安装并且autoUpdater已启用。
完整参考文档
- 文档:/docs/plugins/notifications/
- 调试:/docs/plugins/notifications/debugging/
- 源码: https://github.com/Cap-go/capgo/tree/main/packages/capacitor-notifications/
继续使用@capgo/capacitor-notifications
如果您正在使用 使用@capgo/capacitor-notifications 来规划原生推送消息,连接它与 @capgo/capacitor-notifications 来获取实现细节 调试 来进行故障排除 @capgo/capacitor-updater 来进行静默更新检查,并 Capgo 插件目录 对于其他本机插件。