[英]Android notifications grouping not working
我正在嘗試設置通知。
private fun sendNotification(id: Int, name: String) {
val channelId = getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.msg_new_ticket))
.setContentText(name)
.setGroup(GROUP_KEY)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.build()
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(id, notification)
}
通知發送和接收良好,但不會自動分組。 該函數每次都使用唯一的id
調用。 在 Android 8 (API 26) 上測試。
任何想法請為什么分組不起作用?
更新
文檔說
如果同一個應用程序發送四個或更多通知並且沒有指定分組,系統會自動將它們分組在一起。
我已經嘗試發送 5 個通知,但即使定義了一個組,它們仍然沒有分組。
雖然我有 java 代碼,只需稍作更改即可正常工作,您必須首先為舊設備創建摘要通知,而較新的設備將僅使用此代碼中的“.setSummartText”...
所以,總結通知:
Notification summaryNotification = new NotificationCompat.Builder(this,
App.CHANNEL_1)
.setSmallIcon(R.drawable.o)
.setStyle(new NotificationCompat.InboxStyle()
.addLine(" Olicia : Hey!,what's the progress of the project")
.addLine("James : Bud,Check This Out ")
.setBigContentTitle("2 new messages")
.setSummaryText("Friends Group"))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setGroup("example_group")
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)
.setGroupSummary(true)
.build();
然后添加其他通知
最后,按以下順序通知通知管理器
{首先是摘要通知
然后所有其他通知}
代碼:
notificationManagerCompat.notify(6, summaryNotification);
notificationManagerCompat.notify(2, notification1);
notificationManagerCompat.notify(3, notification2);
notificationManagerCompat.notify(4,notification3);
notificationManagerCompat.notify(5,notification4);
由於一些奇怪的原因,android 需要幾秒鍾的時間來對通知進行分組
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.