Если нотификация показана единственной в списке, то пропадает ее вид. Но как только появляется еще одна - сразу отображается как нужно. Как исправить?
А вот так отображается если уже есть какие-то уведомления (Так должна отображаться)
Вот код:
private fun startNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_ID, NOTIFICATION_NAME, importance)
(applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
}
val notificationBuilder = NotificationCompat.Builder(applicationContext, CHANNEL_ID).also {
it.setCustomContentView(getNotificationPanelRemoteViews())
it.setSmallIcon(R.drawable.ic_launcher_background)
it.setPriority(NotificationCompat.PRIORITY_MAX)
it.setDefaults(Notification.DEFAULT_ALL)
it.setSilent(true)
it.setShowWhen(false)
}
val notification = notificationBuilder.build()
notification.flags = notification.flags or (Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT)
(applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).notify(NOTIFICATION_ID, notification)
}
private fun getNotificationPanelRemoteViews(): RemoteViews {
val flags = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else -> PendingIntent.FLAG_UPDATE_CURRENT
}
val trashButtonIntent = Intent(applicationContext, MainActivity::class.java)
trashButtonIntent.action = CLICK_TRASH
val trashButtonPendingIntent: PendingIntent = PendingIntent.getActivity(
applicationContext, 0,
trashButtonIntent, flags)
val boostButtonIntent = Intent(applicationContext, MainActivity::class.java)
boostButtonIntent.action = CLICK_BOOST
val boostButtonPendingIntent: PendingIntent = PendingIntent.getActivity(
applicationContext, 0,
boostButtonIntent, flags)
val energySavingButtonIntent = Intent(applicationContext, MainActivity::class.java)
energySavingButtonIntent.action = CLICK_ENERGY_SAVING
val energySavingButtonPendingIntent: PendingIntent = PendingIntent.getActivity(
applicationContext, 0,
energySavingButtonIntent, flags)
val portraitCollapsedRemoteView = RemoteViews(applicationContext.packageName, R.layout.notification_panel_layout)
portraitCollapsedRemoteView.setOnClickPendingIntent(R.id.button, trashButtonPendingIntent)
portraitCollapsedRemoteView.setOnClickPendingIntent(R.id.button2, boostButtonPendingIntent)
portraitCollapsedRemoteView.setOnClickPendingIntent(R.id.button3, energySavingButtonPendingIntent)
return portraitCollapsedRemoteView
}
setCustomContentView
есть ещеsetCustomBigContentView
иsetCustomHeadsUpContentView
. Пробовали эти методы?