如何在 Android 中使用 SetGroup() 在 Group 中显示通知? [英] How to display notification in Group using SetGroup() in Android?

查看:114
本文介绍了如何在 Android 中使用 SetGroup() 在 Group 中显示通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用0"通知 ID 以及唯一通知 ID.也使用 setGroup() 如下所示.它仍然每次都会生成一个新通知.我想合并通知正文并将标题设置为通用.

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

我在这里做错了什么吗?另外,我已经通过了这个答案.setgroup() 在通知中不起作用

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

推荐答案

最后我使用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()
}

这篇关于如何在 Android 中使用 SetGroup() 在 Group 中显示通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆