跳过主要内容
返回插件
@capgo/capacitor-notifications
教程
@capgo/capacitor-notifications

通知

使用Capgo发送原生iOS和Android推送通知,支持用户查找、徽章、统计和静默更新检查

指南

__CAPGO_KEEP_0__通知教程

使用capgo/capacitor-notifications

从Capgo发送原生iOS和Android推送通知,支持用户查找、徽章、前台/打开跟踪、后台回调和静默Capgo实时更新检查。

该插件是为Capgo通知管道打造的。Capgo在Postgres中存储平台凭据元数据和推送活动控制数据,而设备状态和传递事件则写入Cloudflare分析引擎。

安装

使用CapgoCLI进行引导式设置:

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。
  • 确认设备已启用平台 androidios 确认设备已启用平台
  • registrationChanged, notificationReceived, notificationOpened添加临时监听器 backgroundNotification.
  • 从控制台发送一个测试通知。
  • 查看排队、发送、接收和打开事件的统计数据。
  • 对于静默的更新检查,验证 @capgo/capacitor-updater 是否已安装并 autoUpdater 是否已启用。

完整参考

继续使用@capgo/capacitor-notifications

如果您正在使用 使用@capgo/capacitor-notifications 来规划原生推送消息,连接它与 @capgo/capacitor-notifications 来实现详细信息 调试 来解决问题 @capgo/capacitor-updater 来进行静默更新检查,并 Capgo插件目录 来其他原生插件