コンテンツにスキップ

Android向けライブ更新

Braze SDKでAndroidライブ更新(Progress Centric Notificationsとも呼ばれます)を使用する方法について説明します。これらの通知はSwift Braze SDKのライブアクティビティに似ており、インタラクティブなロック画面通知を表示できます。Android 16では進行状況を中心とした通知が導入され、ユーザーが開始した最初から最後までのジャーニーをシームレスに追跡できるようになります。

仕組み

IBrazeNotificationFactory インターフェイスを使用して、Brazeプッシュ通知の表示方法をカスタマイズできます。BrazeNotificationFactory を拡張すると、通知がユーザーに表示される前に、Brazeがファクトリの createNotification() メソッドを呼び出します。その後、Brazeダッシュボードまたは REST APIを通じて送信されたカスタムキーと値のペアを含むペイロードが渡されます。

Live Updateの表示

このセクションでは、野生動物レスキューチームが最も多くのフクロウを救えるかを競い合う新しいゲームショーの司会者、Superb Owlと一緒に作業を進めます。彼らはAndroidアプリでLive Updatesを活用し、進行中の試合のステータスを表示して、通知をリアルタイムでダイナミックに更新したいと考えています。

AndroidのLive Updateの例

前提条件

この機能を使用する前に、Android Braze SDKを統合する必要があります。

ステップ1:カスタム通知ファクトリーを作成する

アプリケーションで、BrazeNotificationFactoryを拡張するMyCustomNotificationFactory.ktという名前の新しいファイルを作成し、Braze Live Updatesの表示方法を制御します。

以下の例では、Superb Owlが進行中の試合のLive Updateを表示するカスタム通知ファクトリーを作成しています。次のステップでは、チームのデータをアクティビティにマッピングするgetTeamInfoという新しいメソッドを作成します。

class MyCustomNotificationFactory : IBrazeNotificationFactory {
    override fun createNotification(payload: BrazeNotificationPayload): Notification? {
        val notificationBuilder = populateNotificationBuilder(payload)
        val context = payload.context ?: return null

        if (notificationBuilder == null) {
            brazelog { "Notification could not be built. Returning null as created notification." }
            return null
        }
        notificationBuilder.setContentTitle("Android Live Updates").setContentText("Ongoing updates below")
        setProgressStyle(notificationBuilder, context)
        return notificationBuilder.build()
    }

    private fun setProgressStyle(notificationBuilder: NotificationCompat.Builder, context: Context) {
        val style = NotificationCompat.ProgressStyle()
            .setStyledByProgress(false)
            .setProgress(200)
            .setProgressTrackerIcon(IconCompat.createWithResource(context, R.drawable.notification_small_icon))
            .setProgressSegments(
                mutableListOf(
                    NotificationCompat.ProgressStyle.Segment(1000).setColor(Color.GRAY),
                    NotificationCompat.ProgressStyle.Segment(200).setColor(Color.BLUE),
                )
            )
            .setProgressPoints(
                mutableListOf(
                    NotificationCompat.ProgressStyle.Point(60).setColor(Color.RED),
                    NotificationCompat.ProgressStyle.Point(560).setColor(Color.GREEN)
                )
            )

        notificationBuilder.setStyle(style)
    }
}

ステップ2:カスタムデータをマッピングする

MyCustomNotificationFactory.ktで、Live Updateが表示される際のデータ処理用の新しいメソッドを作成します。

Superb Owlは、各チームの名前とロゴを展開されたLive Updatesにマッピングするために以下のメソッドを作成しました:

class CustomNotificationFactory : BrazeNotificationFactory() {
    override fun createNotification(payload: BrazeNotificationPayload): Notification? {
        // Your existing code
        return super.createNotification(payload)
    }

    // Your new method
    private fun getTeamInfo(team: String?): Pair<String, Int> {
        return when (team) {
            "WBF" -> Pair("Wild Bird Fund", R.drawable.team_wbf)
            "OWL" -> Pair("Owl Rehab", R.drawable.team_owl)
            else  -> Pair("Unknown", R.drawable.notification_small_icon)
        }
    }
}

ステップ3:カスタム通知ファクトリーを設定する

アプリケーションクラスで、customBrazeNotificationFactoryを使用してカスタム通知ファクトリーを設定します。

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        // Tell Braze to use your custom factory for notifications
        Braze.customBrazeNotificationFactory = MyCustomNotificationFactory()
    }
}

ステップ4:アクティビティを送信する

/messages/send REST APIエンドポイントを使用して、ユーザーのAndroidデバイスにプッシュ通知を送信できます。

curlコマンドの例

Superb Owlは以下のcurlコマンドを使用してリクエストを送信しました:

curl -X POST "https://BRAZE_REST_ENDPOINT/messages/send" \
  -H "Authorization: Bearer {REST_API_KEY}" \
  -H "Content-Type: application/json" \
  --data '{
    "external_user_ids": ["USER_ID"],
    "messages": {
      "android_push": {
        "title": "WBF vs OWL",
        "alert": "2 to 4 1:33 Q4",
        "extra": {
          "live_update": "true",
          "team1": "WBF",
          "team2": "OWL",
          "score1": "2",
          "score2": "4",
          "time": "1:33",
          "quarter": "Q4"
        },
        "notification_id": "ASSIGNED_NOTIFICATION_ID"
      }
    }
  }'

リクエストパラメーター

キー 説明
REST_API_KEY messages.send権限を持つBraze REST APIキーです。

これはBrazeダッシュボードの設定 > APIキーから作成できます。
BRAZE_REST_ENDPOINT RESTエンドポイントURLです。エンドポイントはお使いのインスタンスのBraze URLによって異なります。
USER_ID 通知を送信するユーザーのIDです。
messages.android_push.title メッセージのタイトルです。デフォルトでは、カスタム通知ファクトリーのライブ通知には使用されませんが、フォールバックとして使用される場合があります。
messages.android_push.alert メッセージの本文です。デフォルトでは、カスタム通知ファクトリーのライブ通知には使用されませんが、フォールバックとして使用される場合があります。
messages.extra カスタム通知ファクトリーがライブ通知に使用するキーと値のペアです。この値には任意の文字列を割り当てることができます。ただし、curlコマンドの例では、デフォルトのプッシュ通知かライブプッシュ通知かを判別するためにlive_updatesが使用されています。
ASSIGNED_NOTIFICATION_ID 選択したユーザーのライブ通知に割り当てたい通知IDです。このIDはこのゲームに対して一意でなければならず、後で既存の通知を更新するために使用する必要があります。

ステップ5:アクティビティを更新する

既存のLive Updateを新しいデータで更新するには、messages.extraに割り当てられた関連するキーと値のペアを変更してから、同じnotification_idを使用して再度/messages/sendエンドポイントを呼び出します。

New Stuff!