NotificationCompat.MediaStyle
class NotificationCompat.MediaStyle : NotificationCompat.Style
| kotlin.Any | ||
| ↳ | androidx.core.app.NotificationCompat.Style | |
| ↳ | androidx.media.app.NotificationCompat.MediaStyle |
NotificationCompat.DecoratedMediaCustomViewStyle |
Notification style for media custom views that are decorated by the system. |
Notification style for media playback notifications. In the expanded form, up to 5 actions specified with #addAction(int, CharSequence, PendingIntent) addAction will be shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to #setLargeIcon(android.graphics.Bitmap) setLargeIcon() will be treated as album artwork. Unlike the other styles provided here, MediaStyle can also modify the standard-size content view; by providing action indices to setShowActionsInCompactView you can promote up to 3 actions to be displayed in the standard view alongside the usual content. Notifications created with MediaStyle will have their category set to CATEGORY_TRANSPORT unless you set a different category using setCategory(). Finally, if you attach a MediaSession.Token using setMediaSession, the System UI can identify this as a notification representing an active media session and respond accordingly (by showing album artwork in the lockscreen, for example). To use this style with your Notification, feed it to setStyle like so:
Notification noti = new NotificationCompat.Builder() .setSmallIcon(R.drawable.ic_stat_player) .setContentTitle("Track title") .setContentText("Artist - Album") .setLargeIcon(albumArtBitmap)) .setStyle(new NotificationCompat.MediaStyle() .setMediaSession(mySession)) .build();
| See also | |
|---|---|
bigContentView |
Summary
Public constructors |
|---|
MediaStyle(builder: NotificationCompat.Builder!) |
Public functions |
|
|---|---|
java-static MediaSessionCompat.Token! |
getMediaSession(notification: Notification!)Extracts a |
NotificationCompat.MediaStyle! |
setCancelButtonIntent(pendingIntent: PendingIntent!)Sets the pending intent to be sent when the cancel button is pressed. |
NotificationCompat.MediaStyle! |
setMediaSession(token: MediaSessionCompat.Token!)Attaches a |
NotificationCompat.MediaStyle |
@RequiresPermission(value = "android.permission.MEDIA_CONTENT_CONTROL")For media notifications associated with playback on a remote device, provide device information that will replace the default values for the output switcher chip on the media control, as well as an intent to use when the output switcher chip is tapped, on devices where this is supported. |
NotificationCompat.MediaStyle! |
setShowActionsInCompactView(actions: IntArray!)Requests up to 3 actions (by index in the order of addition) to be shown in the compact notification view. |
NotificationCompat.MediaStyle! |
setShowCancelButton(show: Boolean)Sets whether a cancel button at the top right should be shown in the notification on platforms before Lollipop. |
Inherited functions |
||||
|---|---|---|---|---|
|
Public constructors
Public functions
getMediaSession
java-static fun getMediaSession(notification: Notification!): MediaSessionCompat.Token!
Extracts a MediaSessionCompat.Token from the extra values in the MediaStylenotification.
| Parameters | |
|---|---|
notification: Notification! |
The notification to extract a |
| Returns | |
|---|---|
MediaSessionCompat.Token! |
The |
setCancelButtonIntent
fun setCancelButtonIntent(pendingIntent: PendingIntent!): NotificationCompat.MediaStyle!
Sets the pending intent to be sent when the cancel button is pressed. See setShowCancelButton.
| Parameters | |
|---|---|
pendingIntent: PendingIntent! |
the intent to be sent when the cancel button is pressed |
setMediaSession
fun setMediaSession(token: MediaSessionCompat.Token!): NotificationCompat.MediaStyle!
Attaches a MediaSessionCompat.Token to this Notification to provide additional playback information and control to the SystemUI.
setRemotePlaybackInfo
@RequiresPermission(value = "android.permission.MEDIA_CONTENT_CONTROL")
fun setRemotePlaybackInfo(
deviceName: CharSequence,
iconResource: @DrawableRes Int,
chipIntent: PendingIntent?
): NotificationCompat.MediaStyle
For media notifications associated with playback on a remote device, provide device information that will replace the default values for the output switcher chip on the media control, as well as an intent to use when the output switcher chip is tapped, on devices where this is supported.
This method is intended for system applications to provide information and/or functionality that would otherwise be unavailable to the default output switcher because the media originated on a remote device.
Also note that this method is a no-op when running on Tiramisu or less.
| Parameters | |
|---|---|
deviceName: CharSequence |
The name of the remote device to display. |
iconResource: @DrawableRes Int |
Icon resource, of size 12, representing the device. |
chipIntent: PendingIntent? |
PendingIntent to send when the output switcher is tapped. May be |
| Returns | |
|---|---|
NotificationCompat.MediaStyle |
MediaStyle |
setShowActionsInCompactView
fun setShowActionsInCompactView(actions: IntArray!): NotificationCompat.MediaStyle!
Requests up to 3 actions (by index in the order of addition) to be shown in the compact notification view.
| Parameters | |
|---|---|
actions: IntArray! |
the indices of the actions to show in the compact notification view |
setShowCancelButton
fun setShowCancelButton(show: Boolean): NotificationCompat.MediaStyle!
Sets whether a cancel button at the top right should be shown in the notification on platforms before Lollipop.
Prior to Lollipop, there was a bug in the framework which prevented the developer to make a notification dismissable again after having used the same notification as the ongoing notification for a foreground service. When the notification was posted by startForeground, but then the service exited foreground mode via stopForeground, without removing the notification, the notification stayed ongoing, and thus not dismissable.
This is a common scenario for media notifications, as this is exactly the service lifecycle that happens when playing/pausing media. Thus, a workaround is provided by the support library: Instead of making the notification ongoing depending on the playback state, the support library provides the ability to add an explicit cancel button to the notification.
Note that the notification is enforced to be ongoing if a cancel button is shown to provide a consistent user experience.
Also note that this method is a no-op when running on Lollipop and later.
| Parameters | |
|---|---|
show: Boolean |
whether to show a cancel button |