捆绑通知取代第一个通知
Bundled notification replaces the first notification
在使用 setGroup() 和 setGroupSummary() 创建捆绑通知时,我遇到了一些关于通知行为的奇怪问题。
所以,作为参考。此示例包含问题:
var isFirstNotificationInGroup = true
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.activeNotifications.forEach {
if (it.notification.group == groupId) {
isFirstNotificationInGroup = false
}
}
}
val builder = NotificationCompat.Builder(this, channelId).apply {
color = resources.getColor(R.color.colorAccent)
priority = NotificationCompat.PRIORITY_MAX
setSmallIcon(R.drawable.ic_dotoo_logo)
setContentTitle(title)
setContentText(body)
setStyle(NotificationCompat.BigTextStyle()
.bigText(body))
setAutoCancel(true)
setCategory(NotificationCompat.CATEGORY_SOCIAL)
setGroup(groupId)
setGroupSummary(isFirstNotificationInGroup)
}
< ... >
with(NotificationManagerCompat.from(this)) {
notify(notificationId, builder.build())
}
会发生什么?
第一条通知将按原样显示。所以这里没有问题。
然后,当我们显示第二个通知时。它取代了第一个。这不应该发生。 不,这不是因为通知 ID。据我所知,这与此无关。
但是,当我们显示第三个(或更多)通知时,捆绑包按预期工作并显示两个(或更多)捆绑通知。但是第一个……没了。
在此先感谢您对我的帮助。
我已通过在 isFirstNotificationInGroup 为真时创建单独的摘要通知来修复它。
这将在发送 'real' 通知之前发送。
在使用 setGroup() 和 setGroupSummary() 创建捆绑通知时,我遇到了一些关于通知行为的奇怪问题。
所以,作为参考。此示例包含问题:
var isFirstNotificationInGroup = true
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
notificationManager.activeNotifications.forEach {
if (it.notification.group == groupId) {
isFirstNotificationInGroup = false
}
}
}
val builder = NotificationCompat.Builder(this, channelId).apply {
color = resources.getColor(R.color.colorAccent)
priority = NotificationCompat.PRIORITY_MAX
setSmallIcon(R.drawable.ic_dotoo_logo)
setContentTitle(title)
setContentText(body)
setStyle(NotificationCompat.BigTextStyle()
.bigText(body))
setAutoCancel(true)
setCategory(NotificationCompat.CATEGORY_SOCIAL)
setGroup(groupId)
setGroupSummary(isFirstNotificationInGroup)
}
< ... >
with(NotificationManagerCompat.from(this)) {
notify(notificationId, builder.build())
}
会发生什么?
第一条通知将按原样显示。所以这里没有问题。 然后,当我们显示第二个通知时。它取代了第一个。这不应该发生。 不,这不是因为通知 ID。据我所知,这与此无关。
但是,当我们显示第三个(或更多)通知时,捆绑包按预期工作并显示两个(或更多)捆绑通知。但是第一个……没了。
在此先感谢您对我的帮助。
我已通过在 isFirstNotificationInGroup 为真时创建单独的摘要通知来修复它。 这将在发送 'real' 通知之前发送。