android - 强制通知分组?

标签 android notifications android-notifications

根据Notification Design Guidelines文档:

如果同一个应用发送四个或更多通知并且没有指定分组,系统会自动将它们分组在一起。

所以我对此进行了测试:
enter image description here

但是,它仅在您有 4 个或更多通知时才有效。我注意到当您只有两个通知时 Messenger 会这样做:
enter image description here

所以我的问题是,我可以告诉 Android 总是像 Messenger 那样对我的通知进行分组吗?或者 Messenger 使用 setGroup()我不想使用的方法...

最佳答案

这是可行的。
这个概念是创建一个组通知构建器,用于对通知进行分组并应用相同的 groupKey到其他通知生成器。并调用NotificationManagerCompat.notify()预先建立群组通知。所以这些通知具有相同的groupKey将组合在一起。
您可以使用 NotificationCompat.Builder.setGroup("GROUP_KEY") 添加组通知生成器和 NotificationCompat.Builder.setGroupSummary(true) .
并添加NotificationCompat.Builder.setGroup("GROUP_KEY")到其他通知。

val groupKey = "GROUP_KEY"

val groupBuilder = NotificationCompat.Builder(context, "CHANNEL_ID")
groupBuilder.apply {
    setAutoCancel(true)
    ...
    setGroupSummary(true)
    setGroup(groupKey)
}

val builder = NotificationCompat.Builder(context, "CHANNEL_ID")
groupBuilder.apply {
    setAutoCancel(true)
    ...
    setGroup(groupKey)
}

val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(NOTIFICATION_ID, groupBuilder.build())
notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, builder.build())

关于android - 强制通知分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43244876/

相关文章:

flutter - 如何使用 flutter 在 Windows 上显示通知?

javascript - Phonegap android ajax 调用在浏览器中工作,而不是在移动设备上工作

ios - CloudKit CKSubscription 通知基础

javascript - react 通知系统

android - Cordova 3.5.0 有时返回 0,对于 navigator.connection.type

android - 在 Android 中收到一些通知后自动启动应用程序

java - 打开新 Activity 的按钮出现问题

android - 在 flutter 中,我使用AlertDialog,它具有2个操作按钮“确定”和“取消”,单击“确定”时,我要转到新屏幕并结束当前屏幕?

android - V4/V13支持lib : Fragment work, ViewPager不?

Android Firebase 通知 (FCM)