This end-to-end example of a simple action of the Custom action based Mobile App Message (Push) shows how to register the action using a JSON file.

Procedure

  1. Create a test.json file in mce.plugins that maps the action to the class that will be called when the action is taken.
{
  'notification-actions': [
    {
    'type': 'test',
    'class': 'com.ibm.mce.samples.gcm.TestAction'
    },
    {
    'type': 'test2',
    'class': 'com.ibm.mce.samples.gcm.TestAction2'
    },
//.. Register Other actions
  ]
}
Filename isn't really important, but must end in .json

  1. Create the classes that handle the action.
{
  "notification-actions": [
    {
    "type": "test",
    "class": "com.ibm.mce.samples.gcm.TestAction"
    },
    {
    "type": "test2",
    "class": "com.ibm.mce.samples.gcm.TestAction2"
    },
//.. Register Other actions
  ]
}
Filename isn't really important, but must end in .json

    Create the classes that handle the action

TestAction.java (similarly TestAction2.java for the 2nd custom action)
package com.ibm.mce.samples.gcm;
import android.content.Context;
import android.os.Bundle;
import com.ibm.mce.sdk.api.notification.NotificationDetails;
import com.ibm.mce.sdk.api.notification.MceNotificationAction;
import org.json.JSONObject;
import java.util.Map;

public class TestAction implements MceNotificationAction {
    private static final String TAG = "TestAction";
   /**
     * This method implements the "test" action.
     * @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 time value.
     */
    @Override
    public void handleAction(Context context, String type, String name, String attribution, Map<String, String> payload, boolean fromNotification) {
        System.out.println(TAG + ": " + payload.toString());
    }
    @Override
    public void init(Context context, JSONObject initOptions) {
    }
    @Override
    public void update(Context context, JSONObject updateOptions) {
    }
    @Override
    public boolean shouldDisplayNotification(Context context, NotificationDetails notificationDetails, Bundle sourceBundle) {
        return true;
    }
}
  1. In Watson™ Campaign Automation, create the action that sends the appropriate fields.

"{
'label': 'Test Action',
'type': 'test',
'inputRequired': true,
'value': null,
'description': 'Prints the action',
'placeholder': 'Enter placeholder',
'templateId': 1232,
'authenticationRequired': false,
'activationMode': false
},
{
'label': 'Test Action2',
'type': 'test2',
'inputRequired': true,
'value': null,
'description': 'Prints the action',
'placeholder': 'Enter placeholder',
'templateId': 1232,
'authenticationRequired': false,
'activationMode': false
},

Note: The ‘type’ in the .json must match ‘type’ in the action.

 

  1. Add your action to a notification (Example shows using a simple notification). It is not necessary to add the action to the Android manifest.
  2. Click the action to accept it. The following is displayed:
08-22 14:16:54.946 30209-30209/com.ibm.mce.samples.gcm I/System.out: TestAction: {value={}, com.ibm.mce.sdk.NOTIF_SOURCE_PAYLOAD={'attribution':'Simple with my action'}, com.ibm.mce.sdk.NOTIF_SOURCE={'subject':'Simple title','message':'Simple message','notification-action':{'name':'Test Action','type':'test','value':{}},'highPriority':false,'sensitive':false}, com.ibm.mce.sdk.NOTIF_SOURCE_ID=404167564}

The payload can be queried.

JSON File and Custom action handler java file tree structure in android studio

Join The Discussion

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