__CAPGO_KEEP_1__
@capgo/capacitor-通知 设备API 开源

通知 Capacitor 插件

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

安装

bun add @capgo/capacitor-notifications bunx cap sync
npm、pnpm 或 yarn
  • npm install @capgo/capacitor-notifications
  • pnpm add @capgo/capacitor-notifications
  • yarn add @capgo/capacitor-notifications

指南

如何在Capacitor中使用通知

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

注册已签名用户:

mint identityProof 从您的后端生成一个签名,然后在您的登录成功后注册设备。 POST /notifications/recipients/proof监听事件:

await CapgoNotifications.register({
  externalId: 'customer-user-123',
  identityProof,
  tags: ['paid'],
  attributes: { plan: 'team' },
  consent: true,
})

iOS 设置:

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()
  }
})

启用:

推送通知 页面/区域: Capgo 营销网站。角色: 短 UI 标签或导航项。消息键 `push_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.*.
  • 在 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 插件目录 对于其他本机插件。

常见问题

通知插件常见问题

如何在Capacitor应用中安装通知插件?

运行“bun add @capgo/capacitor-notifications”(或“npm install @capgo/capacitor-notifications”),然后运行“bunx cap sync”,使iOS和Android项目捕获native code。从“@capgo/capacitor-notifications”中导入它到您的应用code。

@capgo/capacitor-notifications与React、Vue和Angular兼容吗?

是的。通知是一个Capacitor包,因此它与任何运行在Capacitor中的Web框架兼容,包括Ionic、React、Vue、Angular、Svelte和普通JavaScript。

哪个Capacitor版本支持@capgo/capacitor-notifications?

Capgo插件遵循Capacitor的主要版本:使用与Capacitor主要版本匹配的插件主要版本(例如插件v8与Capacitor 8)。在GitHub README中列出的兼容性表格列出了维护的版本。

@capgo/capacitor-notifications是否免费且开源?

是的。code源代码在GitHub上公开,网址为https://github.com/Cap-go/capgo/tree/main/packages/capacitor-notifications/,并且该包可以从npm免费安装。欢迎bug报告和pull请求。

是否可以在没有App Store审查的情况下更新使用通知的code?

安装或升级插件会改变本地code,因此需要新建一个商店构建。之后,调用插件的JavaScript、HTML和CSS更改可以立即与Capgo实时更新一起发布。

不必等待应用程序审查就可以将通知的更改推送给用户

一旦插件在您的商店构建中,Capgo即刻更新将推送您的JavaScript、HTML和CSS更改给用户

从Capgo开始