How to combine all app notifications together?

441 Views Asked by At

I want to show the app name on top and all the notifications of the app clubbed below it. I am receiving the notification from firebase and using this code:

  val messagingStyle = Notification.MessagingStyle(person)
    val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).also {
        it.setLabel(context.getString(R.string.type_to_reply))
    }.build()
    val mChannel =
        NotificationChannel(channelId, newMessage.text, NotificationManager.IMPORTANCE_HIGH)
    mChannel.description = newMessage.text.toString()
    mChannel.enableVibration(true)
    val notifManager =
        context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notifManager.createNotificationChannel(mChannel)

    val statusBarNotification = notifManager.activeNotifications.firstOrNull {
        it.id == channelId.toInt()
    }
    if (statusBarNotification == null) {
        val builder: Notification.Builder = Notification.Builder(context, channelId)
        val pendingIntent: PendingIntent =
            PendingIntent.getActivity(
                context,
                channelId.toInt(),
                navIntent,
                PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
            )
        builder.setContentTitle(newMessage.text)
            .setSmallIcon(R.drawable.my_logo) // required
            .setContentText(newMessage.text) // required
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true)
            .addAction(
                getNotificationIntent(context, channelId.toInt(), remoteInput)
            )
            .setWhen(newMessage.timestamp)
            .setStyle(messagingStyle.addMessage(newMessage))
            .setContentIntent(pendingIntent)
            .setGroup(MY_NOTIFICATIONS)
            .setGroupSummary(true)
        val notification = builder.build()
        notifManager.notify(channelId.toInt(), notification)
    } else {
        var notificationBuilder = recoverBuilder(context, statusBarNotification.notification)
        notificationBuilder.also {
            val messageStyle = it.style as Notification.MessagingStyle
            messageStyle.addMessage(newMessage.text, newMessage.timestamp, person)
            it.style = messageStyle
        }
        notifManager.notify(channelId.toInt(), notificationBuilder.build())
    }

This is what I want(like whatsapp)

[![whatsapp screenshot]] [1]: https://i.stack.imgur.com/1j3Xf.jpg

But this is how it is appearing right now. all the notifications are seperate.

[![screenshot]] [2]: https://i.stack.imgur.com/UdhrY.jpg

1

There are 1 best solutions below

0
Yuji Bry On

There are similar posts here about merging notifications. They offer different approaches so check them out to see which one is applicable to your use case: