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:
NotificationOptions,
pass an instance of the subclass.MediaIntentReceiver
to handle the intents sent by custom actions.
MediaNotificationManager.updateNotification() to update the notification when
needed. For example, call when the state of the action button changes.In order to provide dynamic custom actions, developers should subclass
NotificationActionsProvider and 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.
NotificationActionsProvider in NotificationOptions
NotificationActionsProvider in NotificationOptions
in order for the SDK to call
NotificationActionsProvider to 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();
}
}
MediaIntentReceiver
and override
MediaIntentReceiver.onReceiveOtherAction(Context, String, Intent). See
MediaIntentReceiver
for more details.
MediaNotificationManager.updateNotification() to update the notification when
needed. See MediaNotificationManager
for more details.
| Context |
getApplicationContext()
Returns the
Context
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.
|
| abstract List<NotificationAction> |
getNotificationActions()
Developers should override this method to return the list of
NotificationAction that will appear in expanded view of the media
notification.
|
Constructs a
NotificationActionsProvider. Developers should use this constructor to
create a new instance.
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).
Developers should override this method to return the list of
NotificationAction that will appear in expanded view of the media
notification. The list may contain at most 5 actions.
NotificationAction in the expanded view of the media notification.