Creating custom actions for mobile app messages

Customize the action template to create your own custom action in Android

Overview

In addition to the standard actions that are provided by Acoustic Campaign, developers can define custom actions and add these actions to mobile app messages. For example, you can define a custom action that opens a special product detail page for customers with loyalty IDs or opens a customer’s abandoned cart.

To help you define your own custom actions, Mobile App Messaging provides a template that you can modify. The template demonstrates how to implement a custom action that prints a payload. Follow the instructions in this tutorial to modify the template for your own custom action.

Prerequisites

Before you begin, ensure that the following prerequisites are met:

  • You have an account with Acoustic Campaign
  • You downloaded the sample app and SDK
  • You properly configured your client app so that it can receive and display mobile app messages

1. Map the custom action to the class that is called when the action is executed

Go to the mobile.push.plugins folder and create a JSON file. The JSON file corresponds to the new plugin that you are adding for the custom action.

  1. Give the file any name that you want but ensure that the file type is .json. For example, the template uses example.json. If you are creating a custom action for a page that opens for a customer’s abandoned cart, you might use cart.json.
  2. In the file, enter the following code, where the “type” is the action that you are creating and the “class” is the class that handles the action plug-in. The following code shows the code that is used in the template JSON file.
{
"notification-actions": [
 {
 "type": "example",
 "class": "co.acoustic.mobile.push.sdk.plugin.example.ExampleAction"
 }
 ]}

For example, if you are creating a custom action for a page that opens for a customer’s abandoned cart, you might enter the following code in the JSON file:

{
"notification-actions": [
 {
 "type": "cart",
 "class": "co.acoustic.mobile.push.sdk .plugin.example.CartAction"
 }
 ]}

2. In your mobile app, create the action class that handles the action plug-in

The action handler for the class performs the action that is specified by your custom action plug-in when the plug-in payload is received.

The template provides the co.acoustic.mobile.push.plugins.example.ExampleAction class that is shown in the following code sample. In the template code, the handleAction method retrieves the payload and the payload’s custom properties, and openForAction starts the activity or sends the payload to the log.

To customize the class for your action plug-in, rename the co.acoustic.mobile.push.plugins.example.ExampleAction class and update the template code. For example, if you are creating a custom action for a page that opens for a customer’s abandoned cart, you might rename the file to co.acoustic.mobile.push.plugins.example.CartAction and update the handleAction to deal with your custom properties and not openForAction and sendCustomEvent.

public void handleAction(Context context, String type, String name, String attribution, String mailingID, Map<String, String> payload, boolean fromNotification) {

   String payloadValue = payload.get("value");
   if (payloadValue == null) {
       Logger.d(TAG, "no payload with value, do nothing");
       return;
   }

   //get custom properties defined in the action
   JSONObject customProperties = null;
   try {
       customProperties = new JSONObject(payloadValue);
   } catch (JSONException exception) {
       Logger.d(TAG, "couldn't parse JSON, giving up");
       return;
   }

   if (customProperties == null) {
       //The JSONObject code exception should handle this case, but if it doesn't, make sure we don't crash
       Logger.d(TAG, "couldn't acquire custom properties, giving up");
       return;
   }

   Logger.d(TAG, "customProperties = " + customProperties);
   boolean sendCustomEvent = customProperties.optBoolean("sendCustomEvent", true);
   boolean openForAction = customProperties.optBoolean("openForAction", true);
   Logger.d(TAG, "sendCustomEvent = " + sendCustomEvent + ", openForAction = " + openForAction);

   //start activity
   Intent intent = new Intent(context, ExampleActivity.class);
   intent.putExtra("payload", (Serializable) payload);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.putExtra("sendCustomEvent", sendCustomEvent);
   intent.putExtra("openForAction", openForAction);
   context.startActivity(intent);
}

3. Configure the custom action in Acoustic Campaign

The developer configures the custom action by following these steps in Acoustic Campaign.

  1. Open Acoustic Campaign and go to Development > Actions. The Actions window opens.
  2. In the Android actions box, click the New action button and then select Custom action. The following code is added to Android actions box:
"label": "Custom action",

 "type": "custom",
 "description": "Opens the product detail page for the product specified for this push",
 "customProperties": [
 {
 "id": "hasLoyaltyId",
 "inputRequired": true,
 "type": "boolean",
 "placeholder": "Does the customer have a loyalty ID?",
 "value": true
 },
 {
 "id": "greeting",
 "inputRequired": true,
 "type": "string",
 "placeholder": "Greeting",
 "value": "hello world"
 },
 {
 "id": "productId",
 "inputRequired": true,
 "type": "number",
 "placeholder": "product id",
 "value": 1234
 },
 {
 "id": "recipients",
 "inputRequired": false,
 "type": "object",
 "placeholder": "",
 "value": [
 "[email protected]",
 "[email protected]"
 ] }
 ],
 "authenticationRequired": false,
 "activationMode": false
 }
 ]
  1. Customize the action by updating the code in the Android actions box. Update “Type” to correspond to the “Type” that was specified in the JSON, and update the custom properties for your custom action. For information about custom properties, see https://help.goacoustic.com/hc/en-us/articles/360044166273-Create-and-set-up-mobile-app-message-templates.

The following code shows how the code is updated for the Example action that is defined in the template. In the following example, the value for “Type” is “example”, which corresponds to “example” in Step 1.

{
 "label": "Example action",
 "type": "example",
 "description": "Prints all the details about push",
 "customProperties": [
 {
 "id": "sendCustomEvent",
 "inputRequired": true,
 "type": "boolean",
 "placeholder": "Send custom event to server?",
 "value": true
 },
 {
 "id": "openForAction",
 "inputRequired": true,
 "type": "boolean",
 "placeholder": "Open view controller?",
 "value": true
 }]}
  1. Save the action.

4. Add the custom action to mobile app messages

After the developer creates the custom action, the marketer can add the action to mobile app messages by following these steps:

  1. In Acoustic Campaign, go to Content > Create Mobile App Message. The Set up your message window opens.
  2. Configure a Simple message type for Android. In the Android content window, add the custom action to the message by clicking the Add action button. The Action window opens.
  3. Select the custom action and click Select action. The Action window closes, and the Set up your message window displays the parameters for the custom action.
  4. Configure the action and then complete the configuration for the mobile app message. You can either send, schedule, or publish the message.