# DefaultMediaNotificationProvider

Artifact: [androidx.media3:media3-session](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/session/DefaultMediaNotificationProvider.java+class:androidx.media3.session.DefaultMediaNotificationProvider)

*** ** * ** ***

<br />

```
@UnstableApi
public class DefaultMediaNotificationProvider implements MediaNotification.Provider
```

<br />

*** ** * ** ***

The default `https://developer.android.com/reference/androidx/media3/session/MediaNotification.Provider`.

## Actions

The following actions are included in the provided notifications:

- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_PLAY_PAUSE()` to start or pause playback.
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_PREVIOUS()` to seek to the previous item.
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_NEXT()` to seek to the next item.

## Custom commands

Custom actions are sent to the session under the hood. You can receive them by overriding the session callback method `https://developer.android.com/reference/androidx/media3/session/MediaSession.Callback#onCustomCommand(androidx.media3.session.MediaSession,androidx.media3.session.MediaSession.ControllerInfo,androidx.media3.session.SessionCommand,android.os.Bundle)`. This is useful because starting with Android 13, the System UI notification sends commands directly to the session. So handling the custom commands on the session level allows you to handle them at the same callback for all API levels.

## Drawables

The drawables used can be overridden by drawables with the same file names in ` res/drawables` of the application module. Alternatively, you can override the drawable resource ID with a `drawable` element in a resource file in `res/values`. The drawable resource IDs are:

- **`media3_icon_play`** - The play icon.
- **`media3_icon_pause`** - The pause icon.
- **`media3_icon_previous`** - The previous icon.
- **`media3_icon_next`** - The next icon.
- **`media3_notification_small_icon`** - The `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setSmallIcon(int)`. A different icon can be set with `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#setSmallIcon(int)`.

## String resources

String resources used can be overridden by resources with the same resource IDs defined by the application. The string resource IDs are:

- **`media3_controls_play_description`** - The description of the play icon.
- **`media3_controls_pause_description`** - The description of the pause icon.
- **`media3_controls_seek_to_previous_description`** - The description of the previous icon.
- **`media3_controls_seek_to_next_description`** - The description of the next icon.
- **`default_notification_channel_name`** The name of the `https://developer.android.com/reference/android/app/NotificationChannel.html` on which created notifications are posted. A different string resource can be set when constructing the provider with `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider.Builder#setChannelName(int)`.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider.Builder` A builder for `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider` instances. |
| `public interface https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider.NotificationIdProvider` Provides notification IDs for posting media notifications for given media sessions. |

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#COMMAND_KEY_COMPACT_VIEW_INDEX() = "androidx.media3.session.command.COMPACT_VIEW_INDEX"` An extras key that can be used to define the index of a `https://developer.android.com/reference/androidx/media3/session/CommandButton` in `https://developer.android.com/reference/android/app/Notification.MediaStyle.html#setShowActionsInCompactView(int...)`. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#DEFAULT_CHANNEL_ID() = "default_channel_id"` The default ID used for the `https://developer.android.com/reference/android/app/NotificationChannel.html` on which created notifications are posted on. |
| `static final @https://developer.android.com/reference/androidx/annotation/StringRes int` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#DEFAULT_CHANNEL_NAME_RESOURCE_ID()` The default name used for the `https://developer.android.com/reference/android/app/NotificationChannel.html` on which created notifications are posted on. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#DEFAULT_NOTIFICATION_ID() = 1001` The default ID used for the `https://developer.android.com/reference/androidx/media3/session/MediaNotification#notificationId()`. |
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#GROUP_KEY() = "media3_group_key"` The group key used for the `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setGroup(java.lang.String)` to avoid the media notification being auto-grouped with the other notifications. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#DefaultMediaNotificationProvider(android.content.Context)(https://developer.android.com/reference/android/content/Context.html context)` Creates an instance. |
| `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#DefaultMediaNotificationProvider(android.content.Context,androidx.media3.session.DefaultMediaNotificationProvider.NotificationIdProvider,java.lang.String,int)( https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider.NotificationIdProvider notificationIdProvider, https://developer.android.com/reference/java/lang/String.html channelId, int channelNameResourceId )` Creates an instance. |

| ### Public methods |
|---|---|
| `final https://developer.android.com/reference/androidx/media3/session/MediaNotification` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#createNotification(androidx.media3.session.MediaSession,com.google.common.collect.ImmutableList<androidx.media3.session.CommandButton>,androidx.media3.session.MediaNotification.ActionFactory,androidx.media3.session.MediaNotification.Provider.Callback)( https://developer.android.com/reference/androidx/media3/session/MediaSession mediaSession, https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton> mediaButtonPreferences, https://developer.android.com/reference/androidx/media3/session/MediaNotification.ActionFactory actionFactory, https://developer.android.com/reference/androidx/media3/session/MediaNotification.Provider.Callback onNotificationChangedCallback )` Creates a new `https://developer.android.com/reference/androidx/media3/session/MediaNotification`. |
| `https://developer.android.com/reference/androidx/media3/session/MediaNotification.Provider.NotificationChannelInfo` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#getNotificationChannelInfo()()` Returns the info of the notification channel used when creating a notification. |
| `final boolean` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#handleCustomCommand(androidx.media3.session.MediaSession,java.lang.String,android.os.Bundle)(https://developer.android.com/reference/androidx/media3/session/MediaSession session, https://developer.android.com/reference/java/lang/String.html action, https://developer.android.com/reference/android/os/Bundle.html extras)` Handles a notification's custom command. |
| `final void` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#setSmallIcon(int)(@https://developer.android.com/reference/androidx/annotation/DrawableRes int smallIconResourceId)` Sets the small icon of the notification which is also shown in the system status bar. |

| ### Protected methods |
|---|---|
| `int[]` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#addNotificationActions(androidx.media3.session.MediaSession,com.google.common.collect.ImmutableList<androidx.media3.session.CommandButton>,androidx.core.app.NotificationCompat.Builder,androidx.media3.session.MediaNotification.ActionFactory)( https://developer.android.com/reference/androidx/media3/session/MediaSession mediaSession, https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton> mediaButtons, https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder builder, https://developer.android.com/reference/androidx/media3/session/MediaNotification.ActionFactory actionFactory )` Adds the media buttons to the notification builder for the given action factory. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton>` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#getMediaButtons(androidx.media3.session.MediaSession,androidx.media3.common.Player.Commands,com.google.common.collect.ImmutableList<androidx.media3.session.CommandButton>,boolean)( https://developer.android.com/reference/androidx/media3/session/MediaSession session, https://developer.android.com/reference/androidx/media3/common/Player.Commands playerCommands, https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton> mediaButtonPreferences, boolean showPauseButton )` Returns the ordered list of `https://developer.android.com/reference/androidx/media3/session/CommandButton` to be used to build the notification. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/CharSequence.html` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#getNotificationContentText(androidx.media3.common.MediaMetadata)(https://developer.android.com/reference/androidx/media3/common/MediaMetadata metadata)` Returns the content text to be used to build the notification. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/CharSequence.html` | `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#getNotificationContentTitle(androidx.media3.common.MediaMetadata)(https://developer.android.com/reference/androidx/media3/common/MediaMetadata metadata)` Returns the content title to be used to build the notification. |

## Constants

### COMMAND_KEY_COMPACT_VIEW_INDEX

```
public static final String COMMAND_KEY_COMPACT_VIEW_INDEX = "androidx.media3.session.command.COMPACT_VIEW_INDEX"
```

An extras key that can be used to define the index of a `https://developer.android.com/reference/androidx/media3/session/CommandButton` in `https://developer.android.com/reference/android/app/Notification.MediaStyle.html#setShowActionsInCompactView(int...)`.

### DEFAULT_CHANNEL_ID

```
public static final String DEFAULT_CHANNEL_ID = "default_channel_id"
```

The default ID used for the `https://developer.android.com/reference/android/app/NotificationChannel.html` on which created notifications are posted on.

### DEFAULT_CHANNEL_NAME_RESOURCE_ID

```
public static final @StringRes int DEFAULT_CHANNEL_NAME_RESOURCE_ID
```

The default name used for the `https://developer.android.com/reference/android/app/NotificationChannel.html` on which created notifications are posted on.

### DEFAULT_NOTIFICATION_ID

```
public static final int DEFAULT_NOTIFICATION_ID = 1001
```

The default ID used for the `https://developer.android.com/reference/androidx/media3/session/MediaNotification#notificationId()`.

### GROUP_KEY

```
public static final String GROUP_KEY = "media3_group_key"
```

The group key used for the `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setGroup(java.lang.String)` to avoid the media notification being auto-grouped with the other notifications. The other notifications sent from the app shouldn't use this group key.

## Public constructors

### DefaultMediaNotificationProvider

```
public DefaultMediaNotificationProvider(Context context)
```

Creates an instance. Use this constructor only when you want to override methods of this class. Otherwise use `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider.Builder`.

### DefaultMediaNotificationProvider

```
public DefaultMediaNotificationProvider(
    Context context,
    DefaultMediaNotificationProvider.NotificationIdProvider notificationIdProvider,
    String channelId,
    int channelNameResourceId
)
```

Creates an instance. Use this constructor only when you want to override methods of this class. Otherwise use `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider.Builder`.

## Public methods

### createNotification

```
public final MediaNotification createNotification(
    MediaSession mediaSession,
    ImmutableList<CommandButton> mediaButtonPreferences,
    MediaNotification.ActionFactory actionFactory,
    MediaNotification.Provider.Callback onNotificationChangedCallback
)
```

Creates a new `https://developer.android.com/reference/androidx/media3/session/MediaNotification`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/MediaSession mediaSession` | The media session. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton> mediaButtonPreferences` | The media button preferences `https://developer.android.com/reference/androidx/media3/session/MediaSession#setMediaButtonPreferences(androidx.media3.session.MediaSession.ControllerInfo,java.util.List<androidx.media3.session.CommandButton>)`. |
| `https://developer.android.com/reference/androidx/media3/session/MediaNotification.ActionFactory actionFactory` | The `https://developer.android.com/reference/androidx/media3/session/MediaNotification.ActionFactory` for creating notification `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Action`. |
| `https://developer.android.com/reference/androidx/media3/session/MediaNotification.Provider.Callback onNotificationChangedCallback` | A callback that the provider needs to notify when the notification has changed and needs to be posted again, for example after a bitmap has been loaded asynchronously. |

### getNotificationChannelInfo

```
public MediaNotification.Provider.NotificationChannelInfo getNotificationChannelInfo()
```

Returns the info of the notification channel used when creating a notification.

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/MediaNotification.Provider.NotificationChannelInfo` | The `https://developer.android.com/reference/androidx/media3/session/MediaNotification.Provider.NotificationChannelInfo`. |

### handleCustomCommand

```
public final boolean handleCustomCommand(MediaSession session, String action, Bundle extras)
```

Handles a notification's custom command.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/MediaSession session` | The media session. |
| `https://developer.android.com/reference/java/lang/String.html action` | The custom command action. |
| `https://developer.android.com/reference/android/os/Bundle.html extras` | A bundle `https://developer.android.com/reference/androidx/media3/session/SessionCommand#customExtras()`, otherwise `https://developer.android.com/reference/android/os/Bundle.html#EMPTY`. |

| Returns |
|---|---|
| `boolean` | `false` if the action should be delivered to the session as a custom command or `true` if the action has been handled completely by the provider. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/MediaNotification.ActionFactory#createCustomAction(androidx.media3.session.MediaSession,androidx.core.graphics.drawable.IconCompat,java.lang.CharSequence,java.lang.String,android.os.Bundle)` |   |

### setSmallIcon

```
public final void setSmallIcon(@DrawableRes int smallIconResourceId)
```

Sets the small icon of the notification which is also shown in the system status bar.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/DrawableRes int smallIconResourceId` | The resource id of the small icon. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setSmallIcon(int)` |   |

## Protected methods

### addNotificationActions

```
protected int[] addNotificationActions(
    MediaSession mediaSession,
    ImmutableList<CommandButton> mediaButtons,
    NotificationCompat.Builder builder,
    MediaNotification.ActionFactory actionFactory
)
```

Adds the media buttons to the notification builder for the given action factory.

The list of `mediaButtons` is the list resulting from `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#getMediaButtons(androidx.media3.session.MediaSession,androidx.media3.common.Player.Commands,com.google.common.collect.ImmutableList<androidx.media3.session.CommandButton>,boolean)`.

Override this method to customize how the media buttons `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#addAction(androidx.core.app.NotificationCompat.Action)` to the notification and define which actions are shown in compact view by returning the indices of the buttons to be shown in compact view.

By default, the buttons for `https://developer.android.com/reference/androidx/media3/common/Player#seekToPreviousMediaItem()`, `https://developer.android.com/reference/androidx/media3/common/Player#play()` or `https://developer.android.com/reference/androidx/media3/common/Player#pause()`, `https://developer.android.com/reference/androidx/media3/common/Player#seekToNextMediaItem()` are shown in compact view, unless some of the buttons are marked with `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#COMMAND_KEY_COMPACT_VIEW_INDEX()` to declare the index in compact view of the given command button in the button extras.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/MediaSession mediaSession` | The media session to which the actions will be sent. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton> mediaButtons` | The command buttons to be included in the notification. |
| `https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder builder` | The builder to add the actions to. |
| `https://developer.android.com/reference/androidx/media3/session/MediaNotification.ActionFactory actionFactory` | The actions factory to be used to build notifications. |

| Returns |
|---|---|
| `int[]` | The indices of the buttons to be `https://developer.android.com/reference/android/app/Notification.MediaStyle.html#setShowActionsInCompactView(int...)`. |

### getMediaButtons

```
protected ImmutableList<CommandButton> getMediaButtons(
    MediaSession session,
    Player.Commands playerCommands,
    ImmutableList<CommandButton> mediaButtonPreferences,
    boolean showPauseButton
)
```

Returns the ordered list of `https://developer.android.com/reference/androidx/media3/session/CommandButton` to be used to build the notification.

This method is called each time a new notification is built.

Override this method to customize the buttons on the notification. Commands of the buttons returned by this method must be contained in `https://developer.android.com/reference/androidx/media3/session/MediaController#getAvailableCommands()`.

By default, the notification shows buttons for `https://developer.android.com/reference/androidx/media3/common/Player#seekToPreviousMediaItem()`, `https://developer.android.com/reference/androidx/media3/common/Player#play()` or `https://developer.android.com/reference/androidx/media3/common/Player#pause()`, `https://developer.android.com/reference/androidx/media3/common/Player#seekToNextMediaItem()` in `https://developer.android.com/reference/android/app/Notification.MediaStyle.html#setShowActionsInCompactView(int...)`. This can be customized by defining the index of the command in compact view of up to 3 commands in their extras with key `https://developer.android.com/reference/androidx/media3/session/DefaultMediaNotificationProvider#COMMAND_KEY_COMPACT_VIEW_INDEX()`.

To make the media button preferences and custom commands work, you need to `https://developer.android.com/reference/androidx/media3/session/MediaSession#setMediaButtonPreferences(androidx.media3.session.MediaSession.ControllerInfo,java.util.List<androidx.media3.session.CommandButton>)` and add the custom commands to the available commands when a controller `https://developer.android.com/reference/androidx/media3/session/MediaSession.Callback#onConnectAsync(androidx.media3.session.MediaSession,androidx.media3.session.MediaSession.ControllerInfo)`. Controllers that connect after you called `https://developer.android.com/reference/androidx/media3/session/MediaSession#setMediaButtonPreferences(androidx.media3.session.MediaSession.ControllerInfo,java.util.List<androidx.media3.session.CommandButton>)` need the custom command set in `https://developer.android.com/reference/androidx/media3/session/MediaSession.Callback#onPostConnect(androidx.media3.session.MediaSession,androidx.media3.session.MediaSession.ControllerInfo)` too.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/MediaSession session` | The media session. |
| `https://developer.android.com/reference/androidx/media3/common/Player.Commands playerCommands` | The available player commands. |
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton> mediaButtonPreferences` | The `https://developer.android.com/reference/androidx/media3/session/MediaSession#setMediaButtonPreferences(androidx.media3.session.MediaSession.ControllerInfo,java.util.List<androidx.media3.session.CommandButton>)`. |
| `boolean showPauseButton` | Whether the notification should show a pause button (e.g., because the player is currently playing content), otherwise show a play button to start playback. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableList.html<https://developer.android.com/reference/androidx/media3/session/CommandButton>` | The ordered list of command buttons to be placed on the notification. |

### getNotificationContentText

```
protected @Nullable CharSequence getNotificationContentText(MediaMetadata metadata)
```

Returns the content text to be used to build the notification.

This method is called each time a new notification is built.

Override this method to customize which field of `https://developer.android.com/reference/androidx/media3/common/MediaMetadata` is used for content text of the notification.

By default, the notification shows `https://developer.android.com/reference/androidx/media3/common/MediaMetadata#artist()` as content text.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/MediaMetadata metadata` | The media metadata from which content text is fetched. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/CharSequence.html` | Notification content text. |

### getNotificationContentTitle

```
protected @Nullable CharSequence getNotificationContentTitle(MediaMetadata metadata)
```

Returns the content title to be used to build the notification.

This method is called each time a new notification is built.

Override this method to customize which field of `https://developer.android.com/reference/androidx/media3/common/MediaMetadata` is used for content title of the notification.

By default, the notification shows `https://developer.android.com/reference/androidx/media3/common/MediaMetadata#title()` as content title.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/MediaMetadata metadata` | The media metadata from which content title is fetched. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/CharSequence.html` | Notification content title. |