Firebase Push Notification (FCM) Payload Guide - beBit-tech/bebit-tech-android-app-sdk GitHub Wiki
When a Firebase Cloud Messaging (FCM) push notification is received by the app, the payload format looks like this:
{
"to": "DEVICE_FCM_TOKEN",
"notification": {
"title": "New Notification",
"body": "Check out this image!",
"sound": "default"
},
"data": {
"image_url": "https://example.com/sample-image.jpg"
}
}Firebase SDK supports displaying images in push notifications. The app must specifically handle data.image_url to display an image in the notification.
Before testing, make sure you have already setup the image_url key/value in omnisegment app push template page.
- Setup Required Dependencies To load images dynamically, Glide must be added to your build.gradle:
dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}- Modify MyFirebaseMessagingService.java to Handle Images
package com.bebit.testApp;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
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";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
String title = remoteMessage.getNotification() != null ? remoteMessage.getNotification().getTitle()
: remoteMessage.getData().get("title");
String body = remoteMessage.getNotification() != null ? remoteMessage.getNotification().getBody()
: remoteMessage.getData().get("body");
String imageUrl = remoteMessage.getData().get("image_url");
showNotification(title, body, remoteMessage.getData(), imageUrl);
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}
private void showNotification(String title, String body, Map<String, String> data, String imageUrl) {
String channelId = "default";
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 | PendingIntent.FLAG_IMMUTABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
channelId,
"Default Channel",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
if (imageUrl != null && !imageUrl.isEmpty()) {
Glide.with(this)
.asBitmap()
.load(imageUrl)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
builder.setLargeIcon(resource);
builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(resource));
NotificationManagerCompat.from(MyFirebaseMessagingService.this).notify(0, builder.build());
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {}
});
} else {
NotificationManagerCompat.from(this).notify(0, builder.build());
}
}