The Android SDK supports the following types of actions.

  • Custom actions that let you implement new action types
  • Text input notifications that prompt users for text input
  • Action plug-ins that let you implement enhanced notifications, such as web display and calendars

Creating custom action notifications

You can create your own custom action notification implementation for new action types.

Custom action notifications override the pre-defined action types in the SDK, such as dial, url, and openApp.

To create custom action notifications:

  1. To create a custom action notification, you must implement com.ibm.mce.sdk.api.notification.MceNotificationAction.
    import com.ibm.mce.sdk.api.notification.MceNotificationAction
    
    public class MyCustomAction implements MceNotificationAction {
    
    /**
     * This method is called when a notification action is clicked.
     * @param context The application context
     * @param type The notification action type
     * @param name The notification action name (can be null)
     * @param attribution The notification attribution (can be null)
     * @param payload The notification payload. The map contains the key value pairs from the notification action payload.
     * @param fromNotification true if this action is called from a notification and false otherwise
     */
    public void handleAction(Context context, String type, String name, String attribution, Map payload, boolean fromNotification) {
    // your implementation here
    }
    
    /**
     * Initiates the action with the given options
     * @param context The application's context
     * @param initOptions The initialization options
     */
    public void init(Context context, JSONObject initOptions){
    
    // your implementation here
    
    }
    
    /**
     * Updates the action with the given options - not is use yet
     * @param context The application's context
     * @param updateOptions The update options
     */
    public void update(Context context, JSONObject updateOptions) {
    
    // your implementation here
    
    }
    /**
     * This method is called when a notification with this action as its main action. If it returns false, the notification will not be shown.
     * This can be used in case there are preparations needed before the notification can be shown.
     * @param context The application's context
     * @param notificationDetails The received notification
     * @param sourceBundle The bundle that contained the notification
     * @return true to show the notification and false to not show it
     */
    public boolean shouldDisplayNotification(Context context, NotificationDetails notificationDetails, Bundle sourceBundle) {
    
    // your implementation here
    
    }
    }
  2. If you want to register your custom action for a specific type, add the following code.
    import com.ibm.mce.sdk.api.notification.MceNotificationActionRegistry
    MceNotificationActionRegistry.registerNotificationAction("snooze", new MyCustomAction());

Creating text input notifications

You can create and send notifications that prompt users for text. When the user receives the input notification, the user clicks an action that expands and displays a text input field. After the user enters text into the field, the notification is canceled.

Android N devices and Android M (and lower devices) require different implementations for text input notifications. For Android N devices, you implement text input notifications by using the input notification action type in an expandable notification action. For Android M devices (and lower), you implement a retrieval mechanism that receives text inputs and cancels the notification.

Android N devices:

Create an input notification action by overriding com.ibm.mce.sdk.api.notification.MceNotificationAction with the following code, which extends com.ibm.mce.sdk.api.notification.MceNotificationAction.Handling with the direct reply:

   public void handleAction(Context context, String input, String type,
    String name, String attribution, Map payload,  boolean fromNotification);
    

Android M (and lower) devices:

  1. Implement com.ibm.mce.sdk.api.notification.MceNotificationInput and return an instance in the following method:
    public MceCustomNotificationInput getCustomInput();
  2. Configure the following method to retrieve the text input and call the action when the user clicks the action.
    public void createCustomInput(Context context, String type, String name,
        String attribution, Map payload) 

Configuring action plug-ins for enhanced notifications

You can use three additional action plug-ins to enhance your mobile app messages: Display Web, Add to Calendar, and Snooze.

In Release 3.6.5.0 of the IBM Mobile App Messaging SDK for Android, plug-in files are delivered in aar and zip file formats. If you are using zip files to implement action plug-ins, add the set of files for each plug-in and register them, as described in Display Web plug-in, Add to Calendar plug-in, and Display Snooze plug-in. Each plug-in requires certain parameters.

If you are using the aar files to implement action plug-ins, add the aar files to your project and to the build.gradle file, and then follow these steps:

  • For the Display Web plug-in, follow steps 5 through 7.
  • For the Add to Calendar plug-in, perform step 2.
  • For the Snooze plug-in, follow steps 2 and 3.

You can find the aar plug-in files in ibm-mobile-push-android-sdk-3.6.5.0/plugins.

Display Web plug-in

You can use the Display Web action plug-in to display a web page within an activity.

  1. Extract the contents of the ibm-mobile-push-android-sdk-plugin-displayweb-<version number>.zip file from the plugins folder.
    • ibm-mobile-push-android-sdk-plugin-displayweb-<version number>.jar is under the bin directory.
    • The plug-in sources are under the src folder.
    • The plug-in Javadoc is under the docs folder.
    • Plug-in resources are under the res folder.

    Note: In Release 3.6.5.0 of the IBM Mobile App Messaging SDK for Android, plug-in files are delivered in the aar and zip file format. You can find the aar plug-in files in ibm-mobile-push-android-sdk-3.6.5.0/plugins.

  2. Copy the resources that are located under res/layout, res/menu, and res/drawable (all the drawable folders) to your project.
  3. Add the strings action_back, action_forward, and action_done from the res/values/strings.xml file to your project’s strings.xml file.
  4. Add ibm-mobile-push-android-sdk-plugin-displayweb-<version number>.jar file from the plugins folder in the SDK package to your project as an Android library.
  5. Register the action with a displayweb type name.
    import com.ibm.mce.sdk.api.notification.MceNotificationActionRegistry;
    import com.ibm.mce.sdk.plugin.displayweb.DisplayWebViewAction;
    MceNotificationActionRegistry.registerNotificationAction("displayWebView", new DisplayWebViewAction());

    Notes:

    • You can use this action to override the SDK openURL action by overriding the url type.
    • When you use the MceNotificationActionRegistry.registerNotificationAction method to register a notification action, the registered callback function is called whether the app is running in the foreground or the background. After the app is terminated, the method is not called for the action provided. To enable the app to react to notification action when the app is not running, use the JSON configuration file method. For more information, see Configuring automatic plug-in detection.
  6. Add the Display Web activity to the AndroidManifest.xml file:
    <activity
        android:name="mce.ibm.com.plugins.DisplayWebViewActivity">
    </activity>
  7. To send an Add to Calendar action, you need to send the following action payload:
    {
    "type": "<the registered type name>", // e.g: "displayweb", "url", ...
    "value": "<the url to display>"
    }

Add to Calendar plug-in

The Add to Calendar plug-in allows a user to add an event to the calendar. This action happens in the background without opening the calendar.

  1. Extract and add the ibm-mobile-push-android-sdk-plugin-calendar-<version number>.zip file from the plugins folder in the SDK package to your project as an Android library.
    • ibm-mobile-push-android-sdk-plugin-calendar-<version number>.jar is under the bin directory.
    • The plug-in sources are under the src folder.
    • The plug-in Javadoc is under the docs folder.

    Note: In Release 3.6.5.0 of the IBM Mobile App Messaging SDK for Android, plug-in files are delivered in the aar and zip file format. You can find the aar plug-in files in ibm-mobile-push-android-sdk-3.6.5.0/plugins.

  2. Register the action with a type name. The following example uses the calendar type name.
    import com.ibm.mce.sdk.api.notification.MceNotificationActionRegistry;
    import com.ibm.mce.sdk.plugin.calendar.AddToCalendarAction;
    
    MceNotificationActionRegistry.registerNotificationAction("calendar", new AddToCalendarAction());

    To send an Add to Calendar action, you need to send the following action payload.

    {
    "type": "<the registered type name>", // e.g: "calendar"
    "title": "<the event title>",
    "description": "<the event description>",
    "starts": "{ // the event start time
    "date": "<the event start date>", // a string date in format "yyyy-MM-dd"
    "time": "<the event start time>", // a string time in format "HH:mm"
    "timezone": "<the event start timezone>", // the timezone id, e.g: "GMT", "EST", etc.
    }
    "ends": "{ // the event end time
    "date": "<the event end date>", // a string date in format "yyyy-MM-dd"
    "time": "<the event end date>", // a string time in format "HH:mm"
    "timezone": "<the event end timezone>", // the timezone id, e.g: "GMT", "EST", etc.
    }
    }

Snooze plug-in

This plug-in contains the Snooze notification action. This action removes the notification from the notification bar and restores it after a number of minutes.

  1. Extract and add the ibm-mobile-push-android-sdk-plugin-snooze-<version number>.zip file from the plugins folder in the SDK package to your project as an Android library.
    • ibm-mobile-push-android-sdk-plugin-snooze-<version number>.jar is under the bin directory.
    • The plug-in sources are under the src folder.
    • The plug-in Javadoc is under the docs folder.

    Note: In Release 3.6.5.0 of the IBM Mobile App Messaging SDK for Android, plug-in files are delivered in the aar and zip file format. You can find the aar plug-in files in ibm-mobile-push-android-sdk-3.6.5.0/plugins.

  2. Register the action with a type name. The following example uses the snooze type name.
    import com.ibm.mce.sdk.api.notification.MceNotificationActionRegistry;
    import com.ibm.mce.sdk.plugin.snooze.SnoozeAction;
     
    MceNotificationActionRegistry.registerNotificationAction("snooze", new SnoozeAction());
  3. Add the snooze intent service to the AndroidManifest.xml file:
    <service android:name="com.ibm.mce.sdk.plugin.snooze.SnoozeIntentService" />

    To send an Add to Calendar action, you need to send the following action payload.

    {
    "type": "<the registered type name>", //e.g: "snooze"
    "time": "<the snooze time in minutes>"
    }

    Tip: You should not have a single action as Snooze.

Go Back to the Mobile App Messaging home page.

Join The Discussion

Your email address will not be published. Required fields are marked *