1

An application receives push notifications. They can be different (payment, order, chat), so I have to show them all, not replacing old with new. A problem is that in old emulators (API 19) all push notifications are shown separately.

Android 4

In newer emulators they are groupped into one, but (if I'm not mistaken) after 3 received notifications.

Android 11

Data sent from FCM:

{
  "to": "<push token>",
  "data": {
    "title": "Application name",
    "message": "See a message",
    "screen": "Main screen",
    ...
  }
}

MyFirebaseMessagingService:

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    super.onMessageReceived(remoteMessage)

    val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val textStyle = NotificationCompat.BigTextStyle()
    val title: String
    val body: String
    if (remoteMessage.data.isNullOrEmpty()) {
        title = remoteMessage.notification?.title ?: ""
        body = remoteMessage.notification?.body ?: ""
    } else {
        title = remoteMessage.data["title"] ?: ""
        body = remoteMessage.data["message"] ?: ""
    }
    val notificationBuilder = NotificationCompat.Builder(this,
        getString(R.string.default_notification_channel_id))
        .setContentTitle(title)
        .setContentText(body)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setStyle(textStyle)
        .setSound(soundUri)
        .setSmallIcon(R.drawable.ic_notification_icon)
        .setAutoCancel(true) // Dismiss the notification on click.

    val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // Since android Oreo notification channel is needed.
    setupChannels(notificationManager, notificationBuilder)

    // Create pending intent, mention the Activity which needs to be triggered
    // when user clicks on notification.
    val notificationId = Random.nextInt()
    navigateToScreen(notificationBuilder, notificationId, remoteMessage)

    val notification = notificationBuilder.build()
    notificationManager.notify(notificationId, notification)
}

private fun setupChannels(notificationManager: NotificationManager,
                          notificationBuilder: NotificationCompat.Builder) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channelId = getString(R.string.default_notification_channel_id)
        val channel = NotificationChannel(channelId, "title",
            NotificationManager.IMPORTANCE_DEFAULT).apply {
            description = "body"
            enableLights(true)
            enableVibration(true)
            lightColor = Color.CYAN
            lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            canShowBadge()
            setShowBadge(true)
        }
        notificationManager.createNotificationChannel(channel)
        notificationBuilder.setChannelId(channelId)
    }
}

private fun navigateToScreen(notificationBuilder: NotificationCompat.Builder,
                             notificationId: Int,
                             remoteMessage: RemoteMessage) {
    val intent = getNavigationIntent(remoteMessage)
    val pendingIntent = PendingIntent.getActivity(this, notificationId, intent,
        PendingIntent.FLAG_UPDATE_CURRENT)
    notificationBuilder.setContentIntent(pendingIntent)
}

private fun getNavigationIntent(remoteMessage: RemoteMessage): Intent {
    val intent = Intent(this, MainActivity::class.java)
    intent.putExtra("screen", remoteMessage.data["screen"])
    return intent
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • Have you tried with similar notification id for all your messages – aryanknp Sep 03 '20 at 17:10
  • @aryanagarwal, yes. It will replace old notifications with new. For instance, https://stackoverflow.com/questions/24439236/android-after-creating-a-new-notification-the-older-one-is-replaced/24439287. You know, there are some interesting topics: https://developer.android.com/training/notify-user/group and https://medium.com/fueled-engineering/collapsing-fcm-notification-like-a-pro-102a4946b350. – CoolMind Sep 03 '20 at 17:19
  • interesting topic though , never thought about it any now and then – aryanknp Sep 03 '20 at 17:37

0 Answers0