通知__CAPGO_KEEP_0__插件:安装、设置与示例
返回插件
@capgo/capacitor-notifications
教程
@capgo/capacitor-notifications

通知

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

指南

通知教程

使用@capgo/capacitor-notifications

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

该插件是为Capgo通知管道而构建的。Capgo在Postgres中存储平台凭据元数据和营销控制数据,而活动设备状态和交付事件则写入Cloudflare分析引擎。

安装

该包当前处于私有预览状态。Capgo必须为您的npm帐户启用包访问才能进行安装。

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

Android 设置

运行 npx cap sync android在 Capgo 中配置 Android 平台凭据,请求 Android 13+ 的通知权限,并在具有 Google Play 服务的设备或模拟器上测试。

调试清单

  • 运行 npx @capgo/cli@latest notifications setup com.example.app 从包含 capacitor.config.*.
  • 通过外部客户端 ID 在 Capgo 中搜索接收者。
  • 确认设备具有 androidios 并已授予权限。
  • 为临时添加监听器 registrationChanged, notificationReceived, notificationOpenedbackgroundNotification.
  • 从控制台发送一个测试通知。
  • 查看排队、发送、接收和打开事件的统计数据。
  • 对于静默的更新检查,验证 @capgo/capacitor-updater 已安装且 autoUpdater 已启用。

完整参考

继续使用@capgo/capacitor-notifications

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