public class

NotificationCompat.MediaStyle

extends NotificationCompat.Style

 java.lang.Object

androidx.core.app.NotificationCompat.Style

↳androidx.media.app.NotificationCompat.MediaStyle

Subclasses:

NotificationCompat.DecoratedMediaCustomViewStyle

Overview

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 NotificationCompat.MediaStyle.setShowActionsInCompactView(int...) 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 using NotificationCompat.MediaStyle.setMediaSession(MediaSessionCompat.Token), 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 NotificationCompat.Builder.setStyle(NotificationCompat.Style) 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();
 

Summary

Fields
from NotificationCompat.StylemBuilder
Constructors
publicMediaStyle()

publicMediaStyle(NotificationCompat.Builder builder)

Methods
public voidapply(NotificationBuilderWithBuilderAccessor builder)

public static android.support.v4.media.session.MediaSessionCompat.TokengetMediaSession(Notification notification)

Extracts a from the extra values in the NotificationCompat.MediaStyle notification.

public RemoteViewsmakeBigContentView(NotificationBuilderWithBuilderAccessor builder)

public RemoteViewsmakeContentView(NotificationBuilderWithBuilderAccessor builder)

public NotificationCompat.MediaStylesetCancelButtonIntent(PendingIntent pendingIntent)

Sets the pending intent to be sent when the cancel button is pressed.

public NotificationCompat.MediaStylesetMediaSession(android.support.v4.media.session.MediaSessionCompat.Token token)

Attaches a to this Notification to provide additional playback information and control to the SystemUI.

public NotificationCompat.MediaStylesetShowActionsInCompactView(int[] actions[])

Requests up to 3 actions (by index in the order of addition) to be shown in the compact notification view.

public NotificationCompat.MediaStylesetShowCancelButton(boolean show)

Sets whether a cancel button at the top right should be shown in the notification on platforms before Lollipop.

from NotificationCompat.StyleaddCompatExtras, applyStandardTemplate, build, buildIntoRemoteViews, clearCompatExtraKeys, createColoredBitmap, displayCustomViewInline, extractStyleFromNotification, getClassName, makeHeadsUpContentView, restoreFromCompatExtras, setBuilder
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public MediaStyle()

public MediaStyle(NotificationCompat.Builder builder)

Methods

public static android.support.v4.media.session.MediaSessionCompat.Token getMediaSession(Notification notification)

Extracts a from the extra values in the NotificationCompat.MediaStyle notification.

Parameters:

notification: The notification to extract a from.

Returns:

The in the notification if it contains, null otherwise.

public NotificationCompat.MediaStyle setShowActionsInCompactView(int[] actions[])

Requests up to 3 actions (by index in the order of addition) to be shown in the compact notification view.

Parameters:

actions: the indices of the actions to show in the compact notification view

public NotificationCompat.MediaStyle setMediaSession(android.support.v4.media.session.MediaSessionCompat.Token token)

Attaches a to this Notification to provide additional playback information and control to the SystemUI.

public NotificationCompat.MediaStyle setShowCancelButton(boolean show)

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 , but then the service exited foreground mode via , 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: whether to show a cancel button

public NotificationCompat.MediaStyle setCancelButtonIntent(PendingIntent pendingIntent)

Sets the pending intent to be sent when the cancel button is pressed. See NotificationCompat.MediaStyle.setShowCancelButton(boolean).

Parameters:

pendingIntent: the intent to be sent when the cancel button is pressed

public void apply(NotificationBuilderWithBuilderAccessor builder)

public RemoteViews makeContentView(NotificationBuilderWithBuilderAccessor builder)

public RemoteViews makeBigContentView(NotificationBuilderWithBuilderAccessor builder)