How to group android Firebase push notifications

Hi Developer in this Android Article, We are discussing How to display multiple notifications as a group?. In Android how to group Android firebase push notification as a channel and group. In Firebase push notification there are two types of  Notification.

Android Firebase Service method onMessageReceived is provided for most message types, with the following exceptions:

  • Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
  • Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

In summary:

App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.

So If you are Send notification In  your server you can use Data Payload for Receiving notification both in App Foreground

and background

How to group android Firebase push notifications.

For Grouping Push notification you just follow these codes.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    Context context;
    NotificationManager notificationManager;
    NotificationCompat.Builder summaryNotificationBuilder;
    int bundleNotificationId = 100;
    int singleNotificationId = 100;
    String GROUP_KEY_WORK_EMAIL = "";

     * Called when message is received.
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
    // [START receive_message]
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be:
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            Object object = remoteMessage.getData().get("payloadExtraData");
            if (object != null) {
                String payloadExtraData = object.toString();
                try {
                    JSONObject jsonObject = new JSONObject(payloadExtraData);
                    String message =jsonObject.getString("message");
                    String title = jsonObject.getString("title");
                        sendNotification(message, title, payloadExtraData);

                } catch (JSONException e) {

            //imageUri will contain URL of the image to be displayed with Notification
            //title for the notification.
            //action string to perform the action e.g. open activity


        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // Instance ID token to your app server.

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            // Log exception
            return null;

    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.


    private void sendNotification(String messageBody, String title, String payloadExtraData) {
        int id = (int) System.currentTimeMillis();
        //  Create Notification
        Bitmap bm = BitmapFactory.decodeResource(getApplication().getResources(), R.drawable.new_logo);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL);

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long[] v = {200,500};
        // Notification Group Key
        String groupKey = "bundle_notification_" + bundleNotificationId;

        //  Notification Group click intent
        Intent resultIntent = new Intent(getApplicationContext(), Notifications_Screen.class);
        resultIntent.putExtra("payloadExtraData", payloadExtraData);
        resultIntent.putExtra("notification_id", bundleNotificationId);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, bundleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // We need to update the bundle notification every time a new notification comes up
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            if (notificationManager.getNotificationChannels().size() < 2) {
                NotificationChannel groupChannel = new NotificationChannel("bundle_channel_id", "bundle_channel_name", NotificationManager.IMPORTANCE_LOW);
                NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
        summaryNotificationBuilder = new NotificationCompat.Builder(this, "bundle_channel_id")
                .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)

        if (singleNotificationId == bundleNotificationId)
            singleNotificationId = bundleNotificationId;

        //  Individual notification click intent
        resultIntent = new Intent(getApplicationContext(), PushNotification_ReDarect.class);
        resultIntent.putExtra("payloadExtraData", payloadExtraData);
        resultIntent.putExtra("notification_id", singleNotificationId);
        resultIntent.setFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        resultPendingIntent = PendingIntent.getActivity(this, singleNotificationId, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
                .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)