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

通知

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

指南

通知教程

使用 @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',
})

注册已签名用户

铸造 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 已启用。

完整参考文档

继续使用 @capgo/capacitor-notifications

如果您正在使用 使用 @capgo/capacitor-notifications 来规划原生推送消息,连接它与 @capgo/capacitor-notifications 来获取实现细节 调试 用于排错 @capgo/capacitor-updater 用于静默更新检查,和 Capgo 插件目录 其他原生插件