2

I have tried using "0" notification id and as well as unique notification id. Also used setGroup() like below. It still generates a new notification every time. I want to merge the notification body and set the title as common.

class MyFirebaseMessagingService : FirebaseMessagingService() {

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

    remoteMessage?.let {
        sendNotification(it.data["alert"])
    }
}

private fun sendNotification(messageBody: String?) {

    val channelId = "${this.packageName}-${this.getString(R.string.app_name)}"

    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

    val builder = NotificationCompat.Builder(this, channelId).apply {
        setDefaults(Notification.DEFAULT_ALL)
        setSmallIcon(if (Build.VERSION.SDK_INT >= 21) R.mipmap.ic_launcher else R.mipmap.ic_launcher_round)
        setContentTitle(getString(R.string.app_name))
        setContentText(messageBody)
        setDefaults(NotificationCompat.DEFAULT_SOUND or NotificationCompat.DEFAULT_VIBRATE or NotificationCompat.DEFAULT_LIGHTS)
        setStyle(NotificationCompat.BigTextStyle().bigText(messageBody))
        priority = NotificationCompat.PRIORITY_DEFAULT
        setAutoCancel(true)
        setSound(defaultSoundUri)
        setGroup(getString(R.string.app_name))
        setGroupSummary(true)
    }

    val manager = getSystemService(NOTIFICATION_SERVICE) as? NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(channelId, getString(R.string.default_channel_name), NotificationManager.IMPORTANCE_HIGH)
        manager?.createNotificationChannel(channel)
    }

    val intent = Intent(this, DashboardFlowActivity::class.java)
    intent.putExtra(DashboardFlowActivity.ISFROMNOTIFICATION, true)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
    builder.setContentIntent(pendingIntent)

    //manager?.cancelAll()

    manager?.notify(this.getString(R.string.app_name), getID(), builder.build())
}
}

private val c = AtomicInteger(0)
private fun getID(): Int {
   return c.incrementAndGet()
}

Anything I am doing wrong here? Also, I have gone through this answer. setgroup() in notification not working

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57

1 Answers1

0

Finally I got solution for this question using https://stackoverflow.com/a/41114135/6021469, https://www.developer.com/ws/android/creating-bundled-notifications-with-android.html

class MyFirebaseMessagingService : FirebaseMessagingService() {

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

    remoteMessage?.let {
        sendNotification(it.data["alert"])
    }
}

private fun sendNotification(messageBody: String?) {

    val channelId = "${this.packageName}-${this.getString(R.string.app_name)}"

    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

    val builder = NotificationCompat.Builder(this, channelId).apply {
        setDefaults(Notification.DEFAULT_ALL)
        setSmallIcon(if (Build.VERSION.SDK_INT >= 21) R.mipmap.ic_launcher else R.mipmap.ic_launcher_round)
        setContentTitle(getString(R.string.app_name))
        setShowWhen(true)
        setContentText(messageBody)
        setDefaults(NotificationCompat.DEFAULT_SOUND or NotificationCompat.DEFAULT_VIBRATE or NotificationCompat.DEFAULT_LIGHTS)
        setStyle(NotificationCompat.BigTextStyle().bigText(messageBody))
        priority = NotificationCompat.PRIORITY_DEFAULT
        setAutoCancel(true)
        setShowWhen(true)
        setSound(defaultSoundUri)
        setGroup(getString(R.string.app_name))
    }

    val builderSummary = NotificationCompat.Builder(this, channelId).apply {
        setDefaults(Notification.DEFAULT_ALL)
        setSmallIcon(if (Build.VERSION.SDK_INT >= 21) R.mipmap.ic_launcher else R.mipmap.ic_launcher_round)
        setContentTitle(getString(R.string.app_name))
        setShowWhen(true)
        setContentText(messageBody)
        setDefaults(NotificationCompat.DEFAULT_SOUND or NotificationCompat.DEFAULT_VIBRATE or NotificationCompat.DEFAULT_LIGHTS)
        setStyle(NotificationCompat.BigTextStyle().bigText(messageBody))
        priority = NotificationCompat.PRIORITY_DEFAULT
        setAutoCancel(true)
        setShowWhen(true)
        setSound(defaultSoundUri)
        setGroup(getString(R.string.app_name))
        setGroupSummary(true)
    }

    val manager = getSystemService(NOTIFICATION_SERVICE) as? NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(channelId, getString(R.string.default_channel_name), NotificationManager.IMPORTANCE_HIGH)
        manager?.createNotificationChannel(channel)
    }

    val intent = Intent(this, DashboardFlowActivity::class.java)
    intent.putExtra(DashboardFlowActivity.ISFROMNOTIFICATION, true)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
    builder.setContentIntent(pendingIntent)


    manager?.notify(this.getString(R.string.app_name), getID(), builder.build())
    manager?.notify(this.getString(R.string.app_name), 0, builderSummary.build())
  }
}

private val c = AtomicInteger(0)
private fun getID(): Int {
   return c.incrementAndGet()
}
Shweta Chauhan
  • 6,739
  • 6
  • 37
  • 57