1

Если нотификация показана единственной в списке, то пропадает ее вид. Но как только появляется еще одна - сразу отображается как нужно. Как исправить?

введите сюда описание изображения

А вот так отображается если уже есть какие-то уведомления (Так должна отображаться)

введите сюда описание изображения

Вот код:

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
}
1
  • Помимо метода setCustomContentView есть еще setCustomBigContentView и setCustomHeadsUpContentView. Пробовали эти методы? 13 дек 2022 в 17:08

1 ответ 1

0

Когда отображается только одно уведомление, оно находится в развернутом состоянии. Чтобы определить внешний вид уведомления в развернутом состоянии, воспользуйтесь методом setCustomBigContentView:

val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
    .setCustomContentView(getNotificationPanelRemoteViews())
    .setCustomBigContentView(getNotificationPanelRemoteViews())
    ...
1
  • Если я так делаю - то появляется возможность развернуть сообщение, и тогда действительно, видны кнопки. Но нужно что бы кнопки всегда были видны. Лучше без возможности развернуть. Вот как на 2 скриншоте (Обведено красным)
    – Camel
    19 дек 2022 в 13:01

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.