NotificationActionsProvider

public abstract class NotificationActionsProvider extends Object

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 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.

Set NotificationActionsProvider in NotificationOptions

Developers should set 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();
     }
 }
 

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. See MediaNotificationManager for more details.

Public Constructor Summary

Public Method Summary

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.

Inherited Method Summary

Public Constructors

public NotificationActionsProvider (Context context)

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

Public Methods

public Context getApplicationContext ()

Returns the Context in 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 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. The list may contain at most 5 actions.

Returns