No expand button in custom notification on Android 10

Viewed 355

I'm trying to build expandable notification with two custom views

Searching in internet says I need to use code like this

val r = RemoteViews(activity.packageName, R.layout.cf_watcher_notification_small)
val r2 = RemoteViews(activity.packageName, R.layout.cf_watcher_notification_big)

val n = NotificationCompat.Builder(activity, "test").apply {
    setSmallIcon(R.drawable.ic_develop)
    setNotificationSilent()
    setShowWhen(false)
    setAutoCancel(true)

    setCustomContentView(r)
    setCustomBigContentView(r2)
    setStyle(NotificationCompat.DecoratedCustomViewStyle())
}

But resulting notification have no expand button that switch collapsed view to big and vice versa, and when I presss to notification nothing happens

enter image description here

Code without big content view

    setCustomContentView(r)
    //setCustomBigContentView(r2)
    setStyle(NotificationCompat.DecoratedCustomViewStyle())

results as expected to

enter image description here

Code with only big content view

    //setCustomContentView(r)
    setCustomBigContentView(r2)
    setStyle(NotificationCompat.DecoratedCustomViewStyle())

results to notification with button I need

enter image description here

enter image description here

How to get this button?

1 Answers

You must use NotificationCompat.BigTextStyle to make it expandable.

Example-

 var notification = NotificationCompat.Builder(activity, "test")
    .setSmallIcon(R.drawable.ic_develop)
    .setNotificationSilent()
    .setShowWhen(false)
    .setAutoCancel(true)
    .setCustomContentView(r)
    .setCustomBigContentView(r2)
    .setStyle(NotificationCompat.BigTextStyle()
            .bigText(SOME_TEXT))
    .build()

You can get more info at here

Related