开始入门
复制一个包含安装步骤和此插件的完整Markdown指南的设置提示.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-notifications`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/notifications/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
@capgo/capacitor-notifications is the first-party Capgo plugin for native iOS and Android push notifications. It is built for Capgo’s dashboard, public API, Analytics Engine device registry, campaign stats, badge updates, and silent live-update checks.
该包目前处于私有预览状态。Capgo 必须为您的 npm 账户启用包访问权限,才能让安装命令正常工作。
需求
需求- A Capacitor app already added to Capgo.
- 访问到Capgo应用的通知标签。
- 一个具有后端证明铸造和API发送权限的CapgoAPI密钥。
- iOS 和/或 Android 平台推送权威服务。
@capgo/capacitor-updater如果您想静默推送更新检查。
1. 配置Capgo 平台凭据
Section titled “1. 配置Capgo 平台凭据”打开应用程序在Capgo,然后前往 通知.
为每个平台添加一个平台凭据条目:
- 安卓 - 应用程序包ID和安卓推送项目元数据。
- iOS - 应用程序ID、团队ID、密钥ID和匹配的iOS推送密钥元数据。
Capgo显示必须在API工作者中存在的exact环境密钥名称。工作台存储元数据和预期密钥引用。私密凭据本身留在工作者环境中。
为了获得最快的设置,请从应用程序项目中运行Capgo CLI:
npx @capgo/cli@latest notifications setup com.example.app命令安装通知包、保存 Capacitor 插件配置、创建一个小型辅助文件,并运行 Capacitor 同步。除非您需要手动连接每个文件,否则使用此路径创建新应用。
手动安装:
npm install @capgo/capacitor-notifications @capgo/capacitor-updaternpx cap sync如果您不使用静默 Capgo 更新检查,则可以省略 @capgo/capacitor-updater.
3. 配置插件
标题:3. 配置插件在应用启动时配置插件一次。
import { CapgoNotifications } from '@capgo/capacitor-notifications'
await CapgoNotifications.configure({ appId: 'com.example.app', autoUpdater: true, updateInstallMode: 'next',})使用 updateInstallMode: 'next' 在下一次重启或后台循环中下载更新并安装。使用 updateInstallMode: 'set' 仅在您希望 Capgo 立即在安全时机安装更新时使用。
4. mint身份证明
4. mint身份证明不要将您的 Capgo API 私钥放入移动应用中。您的后端应要求 Capgo identityProof 在您自己的用户身份验证成功后
curl -X POST 'https://api.capgo.app/notifications/recipients/proof' \ -H 'Content-Type: application/json' \ -H 'x-api-key: CAPGO_API_KEY' \ -d '{ "appId": "com.example.app", "externalId": "customer-user-123" }'返回 identityProof 将
带有您自己的会话响应返回到应用中。
5. 注册设备只有知道当前登录的客户端用户后才能注册。
const registration = await CapgoNotifications.register({ externalId: 'customer-user-123', identityProof, tags: ['paid', 'beta'], attributes: { plan: 'team', locale: 'en-US', }, consent: true,})
console.log(registration.recipientKey, registration.deviceKey)呼叫 register 当:
- 应用程序启动时
- 本机推送令牌发生变化
- 登录用户发生变化
- 标签、属性或同意发生变化
- 应用程序很长时间没有刷新注册
6. 添加事件监听器
标题为“6. 添加事件监听器”在应用程序启动时注册监听器,以便JavaScript可以看到前台、打开和后台事件。
await CapgoNotifications.addListener('registrationChanged', () => { void CapgoNotifications.register({ externalId: currentUser.id, identityProof: currentUser.capgoNotificationProof, tags: currentUser.notificationTags, consent: currentUser.pushConsent, })})
await CapgoNotifications.addListener('notificationReceived', (notification) => { console.log('Notification received', notification)})
await CapgoNotifications.addListener('notificationOpened', (event) => { console.log('Notification opened', event.notification.id)})
await CapgoNotifications.addListener('backgroundNotification', async (event) => { try { console.log('Background notification', event.notification.data) } finally { await event.finish() }})始终调用 finish() 在工作完成后为背景通知调用。保持工作短小且幂等。
7. iOS 设置
第 7 章: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 ios在测试背景通知时,请使用物理iOS设备。模拟器对于UI工作很有用,但不能代表生产背景推送行为。
8. 安卓设置
标题为“8. 安卓设置”运行:
npx cap sync android然后验证:
- 您的安卓平台凭据已在 Capgo 中配置。
- 应用程序包ID与用于平台推送设置的包ID匹配。
- 在Android 13+之前,请求可见通知的权限。
- 应用程序具有与您的品牌匹配的通知图标和通道策略。
- 您可以在物理设备或模拟器上测试,使用Google Play服务。
当您的应用启动时,创建一个默认的Android通道:
await CapgoNotifications.configure({ appId: 'com.example.app' })
await CapgoNotifications.register({ externalId: currentUser.id, identityProof: currentUser.capgoNotificationProof, consent: true,})该插件声明了Android推送消息服务。请在宿主应用中保留应用备份、数据提取、网络安全和通知图标政策。
9. 发送测试通知
标题:9. 发送测试通知使用 通知 > 测试发送 在 Capgo 中复制,或者从您的后端调用公共 API:
curl -X POST 'https://api.capgo.app/notifications/send' \ -H 'Content-Type: application/json' \ -H 'x-api-key: CAPGO_API_KEY' \ -d '{ "appId": "com.example.app", "target": { "externalId": "customer-user-123" }, "payload": { "title": "Hello from Capgo", "body": "This is a test notification.", "data": { "screen": "inbox" } } }'对于一个活动,创建它在控制台或调用 /notifications/campaigns然后将其发送到外部ID、标签、分段或广播受众。
10. 配置徽章
标题:“10. 配置徽章”await CapgoNotifications.setBadge(4)await CapgoNotifications.incrementBadge()await CapgoNotifications.clearBadge()从您的后端:
curl -X POST 'https://api.capgo.app/notifications/badge' \ -H 'Content-Type: application/json' \ -H 'x-api-key: CAPGO_API_KEY' \ -d '{ "appId": "com.example.app", "target": { "externalId": "customer-user-123" }, "badge": 4 }'11. 启用静默更新检查
标题:“11. 启用静默更新检查”静默更新检查将此插件与 @capgo/capacitor-updater.
在应用程序中:
await CapgoNotifications.enableUpdaterIntegration({ enabled: true, installMode: 'next',})在 Capgo 中启用 推送更新给用户 在应用的通知设置中启用,然后从控制台或 API:
curl -X POST 'https://api.capgo.app/notifications/update-check' \ -H 'Content-Type: application/json' \ -H 'x-api-key: CAPGO_API_KEY' \ -d '{ "appId": "com.example.app", "target": { "externalId": "customer-user-123" }, "installMode": "next" }'通知是静默的,并使用折叠 ID,因此当平台支持折叠行为时,重复的更新检查将互相替换。
验证清单
标题为“验证清单”的部分- 应用程序出现在 Capgo 收件人查找中,
externalId. - 权限已获得
granted或用户已接受通知权限。 - 已注册的平台是
android或在令牌刷新后触发。ios. registrationChanged前台测试日志- 打开通知日志
notificationReceived. - 仪表板统计显示排队和已发送的事件,然后在设备报告它们时接收/打开。
notificationOpened. - 静默更新检查记录来自
- 或更新器集成的结果。
runUpdateCheck从开始就继续
标题为“从开始就继续”的部分
如果设置不起作用,请使用调试 在更改应用程序__CAPGO_KEEP_0__之前 before changing app code. Most failures are caused by identity proof mismatch, platform credential setup, OS permission state, background throttling, or app/package ID mismatch.