スタートガイド
インストール手順と、このプラグインの完全な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 は、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-updaterbunx 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. Identity Proof の発行
「4. Identity Proof の発行」セクション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. デバイスの登録
「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. イベントリスナーの追加」アプリ起動時にリスナーを登録し、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() バックグラウンド通知用です。処理は短く、冪等性を持たせてください。
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)}次に実行します:
bunx cap sync iosバックグラウンド通知のテストには実機 iOS デバイスを使用してください。シミュレータは UI 作業には有用ですが、本番環境のバックグラウンドプッシュ動作を再現しません。
8. Android のセットアップ
セクション「8. Android セットアップ」実行:
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 のプッシュメッセージングサービスを宣言します。アプリのバックアップ、データ抽出、ネットワークセキュリティ、通知アイコンのポリシーはホストアプリ側で管理してください。
9. テスト通知の送信
セクション「9. テスト通知を送信する」使用する 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、タグ、セグメント、またはブロードキャストオーディエンスに送信します。
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" }'通知はサイレントで collapse ID を使用するため、プラットフォームが collapse 動作に対応していれば、繰り返しのアップデートチェックは互いに置き換わります。
バリデーション・チェックリスト
セクション「検証チェックリスト」- アプリが Capgo の受信者検索に想定どおり表示される
externalId. - 権限は
grantedまたは、ユーザーが通知権限を許可している。 - 登録されているプラットフォームは
androidまたはios. registrationChangedトークン更新後に発火します。- フォアグラウンドテストではログに
notificationReceived. - 通知を開くとログに記録されます
notificationOpened. - Dashboardの統計には、キューに入ったイベントと送信済みイベントが表示され、デバイスから報告されると受信/開封として表示されます。
- サイレントアップデートのチェックは、次から結果をログに記録します:
runUpdateCheckまたはupdater統合からです。
Getting Startedの続きを進める
「Getting Started からの続き」セクションセットアップがうまくいかない場合は、次を使用してください: デバッグ アプリの code を変更する前に。失敗の多くは、identity proofの不一致、プラットフォームの認証情報設定、OSの権限状態、バックグラウンドでのスロットリング、またはアプリ/パッケージIDの不一致が原因です。