NotificationActionsProvider

public abstract classNotificationActionsProviderextendsObject

If developers want to add dynamic custom actions in the media notification, they should extend this class to provide necessary data to build the media notification. To implement dynamic custom actions, developers should do the following:

Provide notification actions

In order to provide dynamic custom actions, developers should subclass NotificationActionsProviderand override getNotificationActions()and getCompactViewActionIndices().Method getNotificationActions()should return the list of actions that will appear in the expanded view. Method getCompactViewActionIndices()should return the indices of actions that will appear in the compact view.

Developers should also provide a constructor of NotificationActionsProvider(Context)so that the application context can be accessed from this class. Developers can make use of getApplicationContext()to retrieve the context if it helps generate the notification actions.

// MyValidNotificationActionsProvider.java
public class MyValidNotificationActionsProvider extends NotificationActionsProvider {
public MyValidNotificationActionsProvider(@Nonnull Context appContext) {
super(appContext);
}

@Override
public List<NotificationAction> getNotificationActions() {
List<NotificationAction> actions =
new ArrayList<>();
// Add a pre-defined action: play/pause action.
NotificationAction playBackAction = new NotificationAction.Builder()
.setAction(MediaIntentReceiver.ACTION_TOGGLE_PLAYBACK).build();
actions.add(playBackAction);

CastContext castContext = CastContext.getSharedInstance(getApplicationContext());
CastSession castSession = castContext.getSessionManager().getCurrentCastSession();
if (castSession!= null) {
JSONObject customData = mediaStatus.getCustomData();
// Do something with customData.
//...
// Add a custom action.
action = new NotificationAction.Builder()
.setAction( "CUSTOM_ACTION" )
.setIconResId(customActionIconResourceID)
.setContentDescription( "Content description of the custom action." )
.build();
actions.add(action);
}
return actions;
}

@Override
public int[] getCompactViewActionIndices() {
int[] indices = {0, 1};
return indices;
}
}
Note that developers can still use NotificationOptions.Builder.setActions(List, int[])if only pre-defined actions are needed.

Set NotificationActionsProviderinNotificationOptions

Developers should set NotificationActionsProviderinNotificationOptions in order for the SDK to call NotificationActionsProviderto build the notification.
// MyCastOptionsProvider.java
public class MyCastOptionsProvider implements OptionsProvider {
@Override
public CastOptions getCastOptions(Context context) {
NotificationActionsProvider actionsProvider = new MyValidNotificationActionsProvider(
context);
NotificationOptions notificationOptions = new NotificationOptions.Builder()
.setNotificationActionsProvider(actionsProvider)
// Set other fields...
.build();
CastMediaOptions mediaOptions = new CastMediaOptions.Builder()
.setNotificationOptions(notificationOptions)
// Set other fields...
.build();
return new CastOptions.Builder()
.setCastMediaOptions(mediaOptions)
// Set other fields...
.build();
}
}

Handle the custom actions

To handle custom actions, developers should subclass MediaIntentReceiver and override MediaIntentReceiver.onReceiveOtherAction(Context, String, Intent).See MediaIntentReceiver for more details.

Update the notification

If custom actions are not used, the media notification's update is handled by the SDK itself. Otherwise, developers should call MediaNotificationManager.updateNotification()to update the notification when needed. SeeMediaNotificationManager for more details.

Public Constructor Summary

Public Method Summary

Context
getApplicationContext()
Returns theContext in which the app is running.
abstract int[]
getCompactViewActionIndices()
Developers should override this method to returns the indices of actions that will appear in the compact view of the media notification.
abstractList<NotificationAction>
getNotificationActions()
Developers should override this method to return the list of NotificationActionthat will appear in expanded view of the media notification.

Inherited Method Summary

Public Constructors

publicNotificationActionsProvider(Context context)

Constructs a NotificationActionsProvider.Developers should use this constructor to create a new instance.

Public Methods

publicContext getApplicationContext()

Returns theContextin which the app is running.

public abstract int[]getCompactViewActionIndices()

Developers should override this method to returns the indices of actions that will appear in the compact view of the media notification. Each index has to be an integer between 0 (inclusive) and the size of the list returned by getNotificationActions()(exclusive).

Returns
  • The indices of actions in the compact view of the media notification.

public abstractList<NotificationAction> getNotificationActions()

Developers should override this method to return the list of NotificationActionthat will appear in expanded view of the media notification. The list may contain at most 5 actions.

Returns