Android 通知に画像ファイルを添付できます。 ビジュアルコンポーネントを追加すると、プッシュ通知に対するユーザーのエンゲージメントが大幅に向上する可能性があります。
アプリがフォアグラウンドにある場合、プッシュメッセージは FirebaseMessagingService
クラスを拡張するアプリによって処理されます。このクラスは、マニフェストファイルで次のように宣言します。
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
onMessageReceived()
実装を含むクラスは、受信したデータを処理します。
プッシュメッセージにメディア URL が含まれている場合、URL は onMessageReceived()
関数に渡される RemoteMessage
パラメーターで利用できます。使用するキーは、次のコードサンプルに示すように、attachment-url
です。
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("Remote Message", "RemoteMessage: " + remoteMessage.toString());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d("Remote Message", "RemoteMessage: " + remoteMessage.getData());
sendNotification(remoteMessage);
}
}
private void sendNotification(RemoteMessage message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(message.getData().get("body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
//Handle image url if present in the push message
String attachmentUrl = message.getData().get("attachment-url");
if (attachmentUrl != null) {
Bitmap image = getBitmapFromURL(attachmentUrl);
if (image != null) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image));
}
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
NotificationCompat.BigPictureStyle
を設定すると、大きな画像が表示されない場合があります。大きな画像を常に表示するには、ネイティブの Notification.BigPictureStyle
を設定してください。
画像を含むリッチプッシュ通知の例を次に示します。
Android でのリッチプッシュ通知について詳しくは、 リッチ通知を使用したエンゲージ を参照してください。