Firebaseコンソールから通知を送信する際には、以下の手順で行います。
Firebaseコンソールから通知を送信する手順
-
Firebaseコンソールにログイン:
Firebaseコンソールにアクセスし、プロジェクトを選択します。 -
クラウドメッセージングセクションに移動:
プロジェクトの左側メニューから「クラウドメッセージング」を選択します。 -
新しいメッセージを作成:
「通知を送信」ボタンをクリックします。 -
通知の設定:
-
通知のタイトル:
Test Title
(任意のタイトル) -
通知のメッセージ:
Test Message
(任意のメッセージ)
-
通知のタイトル:
-
追加オプションを表示:
「追加オプションを表示」リンクをクリックします。 -
データペイロードの設定:
「カスタムデータ」セクションで、keyとvalueを入力します。-
key:
customNumber
-
value:
123456789
-
key:
-
メッセージのターゲットを設定:
メッセージを送信するターゲットデバイスを選択します(特定のトークン、トピックなど)。 -
メッセージを送信:
「送信」ボタンをクリックしてメッセージを送信します。
Androidアプリ側の実装
上記の通知を受け取るためのAndroidアプリの実装は以下の通りです。
1. MyFirebaseMessagingService.java
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private static final String CHANNEL_ID = "fcm_default_channel";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// データペイロードの処理
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
handleDataPayload(remoteMessage.getData());
}
// 通知ペイロードの処理
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
private void handleDataPayload(Map<String, String> data) {
String customNumber = data.get("customNumber");
Log.d(TAG, "Custom Number: " + customNumber);
// customNumberを使ってアプリで必要な処理を実行
// ここにカスタムデータの処理ロジックを記述
}
private void showNotification(String title, String body) {
createNotificationChannel();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notificationBuilder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Default Channel";
String description = "Default Channel for FCM";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
2. AndroidManifest.xml
<service
android:name=".MyFirebaseMessagingService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
まとめ
Firebaseコンソールから通知を送信する際には、タイトルとメッセージを入力し、追加オプションでデータペイロードを設定できます。アプリ側では、FirebaseMessagingServiceを拡張したクラスで通知とデータペイロードを受信し、適切に処理します。この手順に従って設定することで、keyとvalueに基づいたデータをAndroidアプリで受信し、処理することができます。
受け取るデータがJSON形式で複数のキーと値を含む場合も、FirebaseMessagingServiceを拡張したクラスでそのデータを受信して処理することができます。以下にその手順を示します。
例: JSON形式で複数のキーバリューのデータを送信
送信するデータが以下のようなJSON形式だとします:
{
"to": "YOUR_DEVICE_TOKEN",
"notification": {
"title": "Test Title",
"body": "Test Message"
},
"data": {
"customNumber": "123456789",
"customString": "Hello, World!",
"customBoolean": "true"
}
}
Androidアプリ側の実装
以下のように、複数のキーバリューを受け取って処理するコードを実装します。
1. MyFirebaseMessagingService.java
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private static final String CHANNEL_ID = "fcm_default_channel";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// データペイロードの処理
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
handleDataPayload(remoteMessage.getData());
}
// 通知ペイロードの処理
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
private void handleDataPayload(Map<String, String> data) {
String customNumber = data.get("customNumber");
String customString = data.get("customString");
String customBoolean = data.get("customBoolean");
Log.d(TAG, "Custom Number: " + customNumber);
Log.d(TAG, "Custom String: " + customString);
Log.d(TAG, "Custom Boolean: " + customBoolean);
// 各カスタムデータを使ってアプリで必要な処理を実行
// ここにカスタムデータの処理ロジックを記述
}
private void showNotification(String title, String body) {
createNotificationChannel();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, notificationBuilder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Default Channel";
String description = "Default Channel for FCM";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
2. AndroidManifest.xml
<service
android:name=".MyFirebaseMessagingService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
まとめ
###複数データ
複数のキーバリューを含むJSON形式のデータを受け取る場合も、FirebaseMessagingServiceを拡張したクラスでデータペイロードを受信し、各キーに対する値を処理することができます。上記の実装例では、handleDataPayload
メソッド内で複数のキーバリューをログに出力し、必要に応じてアプリで処理を行います。Firebaseコンソールから送信するデータやAWSから送信するデータがJSON形式であっても、アプリ側の実装は同様に機能します。