__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-notifications

Getting Started

GitHub

@capgo/capacitor-notifications Capgoの第一パーティー プラグインは、ネイティブiOSおよびAndroidのプッシュ通知です。Capgoのダッシュボード、パブリックAPI、Analytics Engineデバイスレジストリ、キャンペーン統計、バッジ更新、静音ライブアップデートチェックに構築されています。

要件

要件
  • CapacitorアプリがCapgoに追加されていること。
  • Capgoアプリの通知タブへのアクセス。
  • CapgoのAPIキーがバックエンドの証明印刷とAPI送信に書き込みアクセス権があること。
  • iOSおよび/またはAndroidプラットフォームのプッシュ権限がアプリに設定されていること。
  • @capgo/capacitor-updater silent push update checks が必要な場合

1. Capgo Platform Credentials を設定する

セクション “1. Capgo Platform Credentials を設定する”

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

各プラットフォームをサポートするために、1 つのプラットフォーム認証情報エントリを追加します:

  • Android - アプリパッケージIDとAndroidプッシュプロジェクトメタデータ。
  • iOS - バンドルID、チームID、キーID、およびiOSプッシュキーと一致するメタデータ。

Capgo は、プラットフォームが設定済みとマークされる前に、API ワーカー内に存在する必要がある環境シークレット名を示します。ダッシュボードはメタデータと期待されるシークレット参照を保存します。プライベートクレデンシャルはワーカー環境内に残ります。

2. インストール

Section titled “2. Install”

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

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

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

手動インストール:

ターミナル画面
npm install @capgo/capacitor-notifications @capgo/capacitor-updater
npx 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' 更新をダウンロードして、次の再起動またはバックグラウンドサイクルでインストールします。使用するには、__CAPGO_KEEP_0__ をすぐにインストールできるようにアップデーターが安全にできる時点でインストールしたい場合のみです。 updateInstallMode: 'set' only when you want Capgo to install the update as soon as the updater can safely do it.

Do not put your Capgo API key in the mobile app. Your backend should ask Capgo for an 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)

「{call}」を呼び出します。 register 「{call}」をもう一度呼びます。

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

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() バックグラウンド通知の後で作業が完了したら呼び出してください。作業は短く、idempotentでなければなりません。

7. iOS設定

7. iOS設定

Xcodeでアプリのターゲットを開き、次の設定を有効にします。

  • Push Notifications
  • コンテキスト:Capgoのマーケティングウェブサイト。役割:短いUIラベルまたはナビゲーションアイテム。メッセージキー`push_notifications` (Push Notifications)。

Background Modes > Remote notifications 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. Androidの設定

8. Androidの設定

次に、以下のコマンドを実行してください。

ターミナル画面
npx cap sync android

次に、以下を確認してください。

  • 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プッシュメッセージングサービスを宣言します。ホストアプリのバックアップ、データ抽出、ネットワークセキュリティ、通知アイコンポリシーを維持してください。

使用 通知>テスト送信 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"
}'

通知は静音で、コラプスIDを使用して、再帰的なアップデートチェックを置き換えます。

  • Capgoの受信者検索で、 externalId.
  • 許可が必要です granted またはユーザーが通知の許可を承認している場合。
  • 登録されたプラットフォームは android または ios.
  • registrationChanged Capacitorライブアップデートの代替オプションの比較ページ。
  • Appflowの比較/移行のマーケティングコピー。 notificationReceived.
  • Capawesomeの比較ページ。 notificationOpened.
  • コンサルティングサービスページ。
  • Appflowの比較/移行のマーケティングコピー。 runUpdateCheck Appflowプラグインまたは

トークン更新後、発生します。

バックグラウンドテストログ

通知を開くログ デバッグ アプリを変更する前に、codeを確認してください。多くの失敗は、アイデンティティの証明の不一致、プラットフォームのクレデンシャル設定、OSのパーミッションの状態、バックグラウンドのサスペンド、またはアプリ/パッケージIDの不一致によって引き起こされます。