指南
通知的教程
使用@capgo/capacitor-notifications
从Capgo发送原生iOS和Android推送通知,支持用户查找、徽章、前台/打开跟踪、后台回调和静默Capgo实时更新检查。
该插件是为Capgo通知管道打造的。Capgo在Postgres中存储平台凭证元数据和推送活动控制数据,而Cloudflare Analytics Engine则记录活动设备状态和传递事件。
安装
使用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 从您的后端 POST /notifications/recipients/proof然后在您的登录成功后注册设备。
await CapgoNotifications.register({
externalId: 'customer-user-123',
identityProof,
tags: ['paid'],
attributes: { plan: 'team' },
consent: true,
})
监听事件
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设置
启用 推送通知 并 背景模式 > 远程通知 在 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)
}
安卓设置
运行 npx cap sync android在Capgo中配置安卓平台凭证,安卓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插件目录 其他原生插件。