指南
通知教程
使用 @capgo/capacitor-notifications
从 Capgo 使用 native iOS 和 Android 推送通知,支持用户查找、徽章、前台/打开跟踪、后台回调和静默 Capgo 实时更新检查。
该插件是为 Capgo 通知管道而构建的。 Capgo 将平台凭据元数据和营销控制数据存储在 Postgres 中,而活动设备状态和传递事件则写入 Cloudflare 分析引擎。
安装
该包目前处于私有预览状态。 Capgo 必须为您的 npm 帐户启用包访问权限才能进行安装。
使用 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',
})
注册已签名用户
从您的后端获取 identityProof Mint POST /notifications/recipients/proof然后在您的登录成功后注册设备。
await CapgoNotifications.register({
externalId: 'customer-user-123',
identityProof,
tags: ['paid'],
attributes: { plan: 'team' },
consent: true,
})
Listen For Events
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()
}
})
iOS 设置
Enable 推送通知 和 Background Modes > Remote 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.*. - Search the recipient in Capgo by external customer ID.
- 通过外部客户端 ID 在__CAPGO_KEEP_0__中搜索收件人。
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 为native推送消息做好准备,连接它 @capgo/capacitor-通知 具体实现细节 调试 故障排除 @capgo/capacitor-更新器 静默更新检查 Capgo 插件目录 其他本地插件