Android用ライブ・アクティビティ
Android Braze SDKでライブ更新をエミュレートする方法を学ぶ。Live Updatesは公式にはサポートされていないが、このガイドでは、Swift Braze SDKのLive Activitiesに似たインタラクティブなロック画面通知を表示できるように、その動作をエミュレートする方法を紹介する。公式のライブ更新とは異なり、この機能は古いバージョンのAndroidにも実装できる。
CDI の仕組み
この IBrazeNotificationFactory
インターフェイスを使って、Brazeプッシュ通知の表示方法をカスタマイズできる。BrazeNotificationFactory
を拡張することで、Brazeはユーザーに通知が表示される前に、ファクトリーのcreateNotification()
メソッドを呼び出す。そして、BrazeダッシュボードまたはREST APIを通して送信されるカスタムキーと値のペアを含むペイロードを渡す。
ライブ更新をエミュレートする
ライブ更新はAndroid OSではネイティブにサポートされていない。以下のセクションでは、彼らの一般的な振る舞いをエミュレートする方法のみを紹介する。
このセクションでは、野生動物救助チームが誰が一番多くのフクロウを救えるかを競う新しいゲーム番組の司会者、スーパーブクロウとパートナーを組むことになる。彼らはAndroidアプリでライブアップデートを活用し、進行中の試合のステータスを表示し、リアルタイムでダイナミックな通知を更新できるようにしようとしている。
前提条件
この機能を使用する前に、Android Braze SDKを統合する必要があります。
ステップ1:顧客レイアウトを追加する
プロジェクトにカスタム・ライブ更新レイアウトを1つ以上追加できる。これらは、折りたたんだり広げたりしたときの通知の表示方法を処理するのに役立つ。ディレクトリ構造は以下のようになっているはずだ:
1
2
3
4
5
6
.
├── app/
└── res/
└── layout/
├── liveupdate_collapsed.xml
└── liveupdate_expanded.xml
各XMLファイルで、カスタムレイアウトを作成する。Superb Owlは、崩壊し拡張されたライブ更新のために以下のレイアウトを作成した:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/notification_title"
style="@style/TextAppearance.Compat.Notification.Title"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
サンプルコードを表示する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/team1logo"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="@drawable/team_default1"/>
<TextView
android:id="@+id/team1name"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1.6"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2-4"
android:textColor="#555555"
android:textAlignment="center"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/timeInfo"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/team2logo"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:src="@drawable/team_default2"/>
<TextView
android:id="@+id/team2name"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
ステップ2: カスタム通知ファクトリーを作成する
アプリケーションで、MyCustomNotificationFactory.kt
という名前の新しいファイルを作成する。 BrazeNotificationFactory
という名前の新しいファイルを作成する。
次の例では、Superb Owlは、進行中の試合のライブ更新を表示するカスタム通知ファクトリーを作成した。次のステップでは、getTeamInfo
という新しいメソッドを作り、チームのデータをアクティビティにマッピングする。
サンプルコードを表示する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import android.app.Notification
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.braze.models.push.BrazeNotificationPayload
import com.braze.push.BrazeNotificationFactory
import com.braze.push.BrazeNotificationUtils.getOrCreateNotificationChannelId
import com.braze.support.BrazeLogger.brazelog
class MyCustomNotificationFactory : BrazeNotificationFactory() {
override fun createNotification(payload: BrazeNotificationPayload): Notification? {
if (payload.extras.containsKey("live_update")) {
val kvp = payload.extras
val notificationChannelId = getOrCreateNotificationChannelId(payload)
val context = payload.context
if (context == null) {
brazelog { "BrazeNotificationPayload has null context. Not creating notification" }
return null
}
val team1 = kvp["team1"]
val team2 = kvp["team2"]
val score1 = kvp["score1"]
val score2 = kvp["score2"]
val time = kvp["time"]
val quarter = kvp["quarter"]
// Superb Owl will define the 'getTeamInfo' method in the next step.
val (team1name, team1icon) = getTeamInfo(team1)
val (team2name, team2icon) = getTeamInfo(team2)
// Get the layouts to use in the custom notification.
val notificationLayoutCollapsed = RemoteViews(BuildConfig.APPLICATION_ID, R.layout.liveupdate_collapsed)
val notificationLayoutExpanded = RemoteViews(BuildConfig.APPLICATION_ID, R.layout.liveupdate_expanded)
// Very simple notification for the small layout
notificationLayoutCollapsed.setTextViewText(
R.id.notification_title,
"$team1 $score1 - $score2 $team2\n$time $quarter"
)
notificationLayoutExpanded.setTextViewText(R.id.score, "$score1 - $score2")
notificationLayoutExpanded.setTextViewText(R.id.team1name, team1name)
notificationLayoutExpanded.setTextViewText(R.id.team2name, team2name)
notificationLayoutExpanded.setTextViewText(R.id.timeInfo, "$time - $quarter")
notificationLayoutExpanded.setImageViewResource(R.id.team1logo, team1icon)
notificationLayoutExpanded.setImageViewResource(R.id.team2logo, team2icon)
val customNotification = NotificationCompat.Builder(context, notificationChannelId)
.setSmallIcon(R.drawable.notification_small_icon)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build()
return customNotification
} else {
// Use the BrazeNotificationFactory for all other notifications
return super.createNotification(payload)
}
}
}
ステップ 3:顧客データをマップする
MyCustomNotificationFactory.kt
で、ライブ更新が表示されたときにデータを処理するための新しいメソッドを作成する。
Superb Owlは、各チームの名前とロゴを拡大されたライブ更新に対応させるために、以下の方法を作成した:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)
}
}
}
ステップ4:カスタム通知ファクトリーを設定する
アプリケーション・クラスで customBrazeNotificationFactory
を使用して、カスタム通知ファクトリを設定する。
1
2
3
4
5
6
7
8
9
10
import com.braze.Braze
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
// Tell Braze to use your custom factory for notifications
Braze.customBrazeNotificationFactory = MyCustomNotificationFactory()
}
}
ステップ 5: アクティビティを送信する
あなたは /messages/send
REST APIエンドポイントを使用して、ユーザーのAndroidデバイスにプッシュ通知を送信できる。
curlコマンドの例
Superb Owlは以下のcurlコマンドを使ってリクエストを送った:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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"
}
}
}'
curlコマンドはテストに役立つが、iOSライブ・アクティビティをすでに処理しているバックエンドでこの呼び出しを処理することをお勧めする。
リクエストパラメーター
キー | 説明 |
---|---|
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 |
カスタム通知ファクトリがライブ通知に使用するキーと値のペア。この値には任意の文字列を割り当てることができるが、上の例では、live_updates 、デフォルトかライブ・プッシュ通知かを判断するために使われている。 |
ASSIGNED_NOTIFICATION_ID |
選択したユーザーのライブ通知に割り当てたい通知ID。このIDはこのゲームに固有のものでなければならず、既存の通知を後で更新するために使用しなければならない。 |
ステップ 6: アクティビティを更新する
既存のライブアップデートを新しいデータで更新するには、messages.extra
に割り当てられた関連するキーと値のペアを修正し、同じnotification_id
を使用し、/messages/send
エンドポイントを再度呼び出す。