【问题标题】:How to display notification in Group using SetGroup() in Android?如何在 Android 中使用 SetGroup() 在组中显示通知?
【发布时间】:2025-11-21 10:10:02
【问题描述】:

我尝试过使用“0”通知 ID 以及唯一通知 ID。 还使用了 setGroup() ,如下所示。它仍然每次都会生成一个新的通知。我想合并通知正文并将标题设置为通用。

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()
}

我在这里做错了什么吗?另外,我已经完成了这个答案。 setgroup() in notification not working

【问题讨论】:

    标签: android kotlin push-notification firebase-cloud-messaging


    【解决方案1】:

    最后我用https://*.com/a/41114135/6021469https://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()
    }
    

    【讨论】:

    • 在代码之外,您没有说出您为解决问题所做的更改...