I am attempting to employ a MediaStyle notification in an audio Android application as discussed in the audio app documentation as well as in the media style documentation. I am able to successfully show a notification with functional transport controls, so I believe my implementation to be sound. However, when I set the androidx.media.app.NotificationCompat.MediaStyle
via the .setStyle()
of the NotificationCompat.Builder
as recommended, it appears to ignore any usage of the .setOngoing()
flag. setOngoing
ensures "notifications cannot be dismissed" according to the source code. I experience the described behavior when no media style is set but not when I use .setStyle()
.
I am wondering if anyone is aware of a workaround or if there is some undocumented requirement I need to appease to use .setOngoing()
with a media style notification. My compile SDK version, target SDK version, and min SDK version are all 30. Please let me know if any additional code would be useful; I believe I am providing what is relevant.
To illustrate this predicament, I've included screenshots showing how the notification is dismissible when the media style is applied and is not dismissible when the media style is not applied.
Dependencies (all up to date):
val appCompatVersion: String = "1.4.0-alpha01"
val mediaVersion: String = "1.4.0-alpha01"
val media2Version: String = "1.0.0-alpha04"
implementation("androidx.appcompat:appcompat:$appCompatVersion")
implementation("androidx.media:media:$mediaVersion")
implementation("androidx.media2:media2:$media2Version")
private val notification: Notification?
get() {
val controller: MediaControllerCompat = mediaSession.controller ?: return null
val description: MediaDescriptionCompat = controller.metadata?.description ?: return null
val notificationManager: NotificationManager = notificationManager ?: return null
val notificationChannel = NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_NONE
)
if (notificationChannel !in notificationManager.notificationChannels) {
notificationManager.createNotificationChannel(notificationChannel)
}
...
val style = androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(controller.sessionToken)
.setShowActionsInCompactView(0, 1, 2)
.setShowCancelButton(false)
return NotificationCompat.Builder(
this,
CHANNEL_ID
).apply {
actions.forEach { addAction(it) }
color = backgroundColor
}
.setContentTitle(description.title)
.setContentText(description.subtitle)
.setSmallIcon(smallIcon)
.setLargeIcon(largeIcon)
.setOngoing(true)
.setColorized(true)
.setAutoCancel(false)
.setAllowSystemGeneratedContextualActions(true)
.setContentIntent(controller.sessionActivity)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setChannelId(CHANNEL_ID)
.setStyle(style)
.build()
}