メインコンテンツへスキップ

スタートガイド

GitHub

@capgo/capacitor-notifications は、iOS/Androidネイティブのプッシュ通知に対応するCapgo純正プラグインです。Capgoのダッシュボード、公開API、Analytics Engineのデバイスレジストリ、キャンペーン統計、バッジ更新、サイレントなLive Updateチェック向けに構築されています。

  • Capgoに既に追加済みのCapacitorアプリ。
  • CapgoアプリのNotificationsタブへのアクセス権限。
  • バックエンドでのproof発行とAPI送信用の、書き込み権限を持つCapgo APIキー。
  • アプリのiOSおよび/またはAndroidプラットフォームにおけるプッシュ通知権限。
  • @capgo/capacitor-updater サイレントプッシュでアップデートチェックを行いたい場合。
import { CapacitorNotifications } from '@capgo/capacitor-notifications';
await CapacitorNotifications.register({ customerUserId: 'signed-id' });
  • バックエンドから取得した署名済みの customerUserId 値を使用してください。
  • 登録前に通知の権限をリクエストしてください。
  • ダッシュボードで Capgo の認証情報を確認してください。

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

セクション「1. Capgo プラットフォーム認証情報の設定」

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

サポートする各プラットフォームごとに、認証情報を1件ずつ追加してください:

  • Androidでは - アプリのパッケージIDとAndroidプッシュプロジェクトのメタデータ。
  • iOSと - bundle ID、team ID、key ID、および対応するiOSプッシュキーのメタデータ。

Capgo には、プラットフォームが設定済みとマークされる前に API worker 内に存在している必要がある環境シークレット名が正確に表示されます。ダッシュボードが保存するのはメタデータと期待されるシークレット参照です。実際の認証情報自体はworkerの環境内に留まります。

2. インストール

「2. Install」セクション

最も素早くセットアップするには、アプリのプロジェクトから Capgo CLI を実行してください:

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

このコマンドは、通知パッケージのインストール、Capacitor プラグイン設定の保存、小さなヘルパーファイルの作成、および Capacitor sync の実行を行います。すべてのファイルを手動で個別に設定する必要がない限り、新規アプリではこの方法を使用してください。

手動インストール:

ターミナル
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 に、アップデータが安全に実行できるようになり次第すぐアップデートをインストールさせたい場合にのみ使用します。

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 独自のセッションレスポンスと共にアプリへ返してください。

どの顧客ユーザーがサインインしているかを確認してから、デバイスを登録してください。

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 次の場合にもう一度:

  • アプリが起動したとき。
  • ネイティブプッシュトークンが変更されたとき。
  • サインイン中のユーザーが変更されたとき。
  • タグ、属性、または同意状態が変更されたとき。
  • アプリが長期間にわたり登録をリフレッシュしていないとき。

アプリ起動時にリスナーを登録し、foreground、opened、background のイベントが 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() バックグラウンド通知用です。処理は短く、冪等性を持たせてください。

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

次に実行します:

ターミナル
bunx cap sync ios

バックグラウンド通知のテストには実機 iOS デバイスを使用してください。シミュレータは UI 作業には有用ですが、本番環境のバックグラウンドプッシュ動作を再現しません。

実行:

ターミナル
bunx cap sync android

次に、以下を確認します:

  • Android プラットフォームの認証情報が Capgo に設定されています。
  • アプリのパッケージ ID が、プラットフォームのプッシュ設定で使用したパッケージ ID と一致しています。
  • 通知を表示する前に、Android 13 以降の通知権限をリクエストしている。
  • アプリには、ブランドに合った通知アイコンとチャネル戦略があります。
  • Google Play services を搭載した実機またはエミュレーターでテストしています。

アプリ起動時にデフォルトの Android チャネルを作成します:

await CapgoNotifications.configure({ appId: 'com.example.app' })
await CapgoNotifications.register({
externalId: currentUser.id,
identityProof: currentUser.capgoNotificationProof,
consent: true,
})

プラグインは Android のプッシュメッセージングサービスを宣言します。アプリのバックアップ、データ抽出、ネットワークセキュリティ、通知アイコンのポリシーはホストアプリ側で管理してください。

使用する Notifications > テスト送信 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()

バックエンドから:

ターミナル
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"
}'

通知はサイレントで collapse ID を使用するため、プラットフォームが collapse 動作に対応していれば、繰り返しのアップデートチェックは互いに置き換わります。

バリデーション・チェックリスト

セクション「検証チェックリスト」
  • アプリが Capgo の受信者検索に想定どおり表示される externalId.
  • 権限は granted または、ユーザーが通知権限を許可している。
  • 登録されているプラットフォームは android または ios.
  • registrationChanged トークン更新後に発火します。
  • フォアグラウンドテストではログに notificationReceived.
  • 通知を開くとログに記録されます notificationOpened.
  • Dashboardの統計には、キューに入ったイベントと送信済みイベントが表示され、デバイスから報告されると受信/開封として表示されます。
  • サイレントアップデートのチェックは、次から結果をログに記録します: runUpdateCheck またはupdater統合からです。

Getting Startedの続きを進める

「Getting Started からの続き」セクション

セットアップがうまくいかない場合は、次を使用してください: デバッグ アプリの code を変更する前に。失敗の多くは、identity proofの不一致、プラットフォームの認証情報設定、OSの権限状態、バックグラウンドでのスロットリング、またはアプリ/パッケージIDの不一致が原因です。