__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-notificationsをインストールし、プラットフォームの資格情報を設定し、デバイスを登録し、テスト通知を送信し、静黙の更新チェックを有効にします。

はじめに

GitHub

@capgo/capacitor-notifications Capgo は、ネイティブ iOS と Android のプッシュ通知用のファーストパーティ プラグインです。Capgo のダッシュボード、パブリック API、分析エンジン デバイス レジストリ、キャンペーン統計、バッジ更新、静的なライブアップデートチェックのために構築されています。

要件

要件
  • Capacitor アプリが Capgo に追加されていること
  • Capgo アプリの通知タブへのアクセス
  • Capgo の API キーがバックエンドの証明印刷と API 送信のために書き込みアクセス権を持っていること
  • iOS と/または Android プラットフォームのプッシュ権限
  • @capgo/capacitor-updater 静的なプッシュアップデートチェックを行う場合

1. Capgoのプラットフォーム認証情報を設定

Section titled “1. Capgoのプラットフォーム認証情報を設定”

Capgoでアプリを開き、次に 通知.

各プラットフォームをサポートするには、プラットフォームごとに 1 つのプラットフォーム認証情報エントリを追加してください。

  • Android Android
  • iOS iOS

Capgo shows the exact environment secret name that must exist in the API worker before the platform is marked configured. The dashboard stores metadata and the expected secret reference. The private credential itself stays in the worker environment.

2. インストール

2. インストール

最速のセットアップのために、Capgo CLI をアプリプロジェクトから実行してください。

ターミナルウィンドウ
npx @capgo/cli@latest notifications setup com.example.app

コマンドは通知パッケージをインストールし、Capacitor プラグインの設定を保存し、小さなヘルパーファイルを作成し、Capacitor 同期を実行します。新しいアプリの場合にファイルを手動で接続する必要がある場合は、このパスを使用してください。

手動インストール:

ターミナルウィンドウ
bun add @capgo/capacitor-notifications @capgo/capacitor-updater
bunx cap sync

静音のCapgo更新チェックを使用していない場合は、省略できます。 @capgo/capacitor-updater.

アプリが起動したときに一度だけプラグインを設定してください。

import { CapgoNotifications } from '@capgo/capacitor-notifications'
await CapgoNotifications.configure({
appId: 'com.example.app',
autoUpdater: true,
updateInstallMode: 'next',
})

使用 updateInstallMode: 'next' を使用して updateInstallMode: 'set' アップデートをインストールするには、Capgo をすぐにアップデートできるようにする必要があります。

4. ID証明書を発行する

「4. ID証明書を発行する」

アプリに自分のCapgo APIキーを入れないでください。バックエンドはCapgoにアクセスしてください。 identityProof ID証明書を発行する

ターミナル画面
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. デバイスを登録する

Section titled “5. Deviceを登録する”

ログインしている顧客ユーザーがわかっているまで、登録しないでください。

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)

Call register または

  • アプリが起動する
  • ネイティブプッシュトークンが変更される
  • ログインしているユーザーが変更される
  • タグ、属性、または同意が変更される
  • アプリが長い間登録の更新を実行していない

アプリ起動時にリスナーを登録して、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設定

プッシュ通知

  • バックグラウンドモード > リモート通知
  • リモート通知を

次に実行してください 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)
}

context

ターミナル画面
bunx cap sync ios

物理iOSデバイスを使用してバックグラウンド通知をテストする。シミュレータはUI作業に便利ですが、生産的なバックグラウンドプッシュの動作を表すものではありません。

8. Androidの設定

8. Androidの設定

実行:

ターミナル画面
bunx cap sync android

次に確認:

  • CapgoでAndroidプラットフォームの資格情報が設定されています。
  • アプリパッケージ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、タグ、セグメント、またはブロードキャストアウディエンスに送信します。

await CapgoNotifications.setBadge(4)
await CapgoNotifications.incrementBadge()
await CapgoNotifications.clearBadge()

コンテキスト: Capgo マーケティング ウェブサイト。役割: UI ラベル。表示: ページエージェント/[slug].astro。メッセージキー `agents_page_copy_label` (エージェント ページ コピー ラベル)。

バックエンドから:
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. 静音アップデートチェックを有効にする」のセクション @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.
  • __CAPGO_KEEP_0__内で許可されている granted またはユーザーが通知許可を受け入れている
  • 登録プラットフォームは android または ios.
  • registrationChanged ライブアップデート
  • 通知がトークン更新後に発生します。 notificationReceived.
  • フロントエンドテストログ notificationOpened.
  • 通知を開くログ
  • Silent update checks log a __CAPGO_KEEP_0__ runUpdateCheck またはアップデーターの統合から結果を取得します。

続けてGetting Startedから進みましょう

「続けてGetting Startedから進みましょう」

セットアップがうまくいかない場合は、デバッグを使用してください 通知 アプリを変更する前に code. 最も多く発生するエラーは、アイデンティティの証明の不一致、プラットフォームの資格情報の設定、OSの許可状態、バックグラウンドのサブスクリプション、またはアプリ/パッケージIDの不一致です。