Custom actions

You can customize the plugins provided to create custom actions or create custom action plugins. You can create your own custom action notification implementation for new action types. Custom action notifications override the predefined action types in the Campaign library, such as dial, URL, and openApp.

Custom action methods are defined by the developer and run when an action is selected by the user with the registered type value. An example of this would be for the developer to create a method that handles viewProduct action types. The developer registers that method with the MCEActionRegistry system. Then, a push message comes in with the "notification-action" set to type="viewProduct", and the app user opens the mobile app message.

Requirements

When a custom action for a mobile app message is used, the marketer must also include the destructive, authentication, and foreground keys in the action definition.

The "aps" section is used for system displayed notifications.

The {{type}} of action can be anything. Predefined action types include "dial" and "url". If the {{type}} is "rich", "dial", or "url", then the destructive is false, authentication is false, and foreground is true; otherwise, those attributes need to be included.

The {{value}} depends on the type of action. If the type is "dial", then the value is a phone number to be dialed. If the type is "url", then the value is the url that is to be viewed. If the type is "rich", then the value is the rich content ID to be displayed. When the type is anything else, it is handled by the plugin that is defined for that type.

The {{name}} of the action is the title that shows on the button. If {{type}} is not "rich", "dial", or "url", the following keys need to be defined in the action:

  • {{destructive}} can be either true or false. This sets the style of the action button.

  • {{authentication}} can be either true or false. This allows actions to be performed with the device is locked when true.

  • {{foreground}} can be either true or false. If it is true, then the app is opened when a selection is made. If it is false, then the app is not opened.

Create custom actions

The Campaign iOS library provides three custom actions that you can implement:

  • Custom action using a single argument type payload
  • Custom action using an action payload and a full iOS payload
  • Custom action for text entry fields

The following example shows implementing a custom action using a single argument type. You can customize your dynamic action category types by registering your object and selector with the MCEActionRegistry class.

- (instancetype)init { if (self = [super init]) { MCEActionRegistry* registry = [MCEActionRegistry sharedInstance]; [registry registerTarget:[self sharedInstance] withSelector:@selector(customAction:) forAction:@"customAction"]; } return self; ]

When a notification is opened with the customAction type, your custom method is run.

- (void)customAction:(NSDictionary*)action { // Do something neat with the action payload here. }

Here is an example of a custom action using an action payload and full iOS payload. This custom action is demonstrated in the Action Menu plugin in the sample app. The following code sample shows how to register the custom action.

MCEActionRegistry* registry = [MCEActionRegistry sharedInstance]; [registry registerTarget:[self sharedInstance] withSelector:@selector(executeAction:withPayload:) forAction:@"custom"];

The following code shows how to implement the custom action.

- (void)executeAction:(NSDictionary*)action withPayload:(NSDictionary*)payload { // the action dictionary contains the action that is being executed, eg: // {"type": "custom", "value":"foo"} // the payload dictionary includes the full APNS payload, e.g. {"aps": {...}} }

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 a custom action notification, you must implement co.acoustic.mobile.push.sdk.api.notification.MceNotificationAction.

import co.acoustic.mobile.push.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 } }

To register your custom action for a specific type, add the following code.

import co.acoustic.mobile.push.sdk.api.notification.MceNotificationActionRegistry MceNotificationActionRegistry.registerNotificationAction("snooze", new MyCustomAction());

Action plugins can be used to extend the behavior executed when a message is interacted with. Push messages, InApp messages and Inbox messages all use actions to define what happens when a user interacts with them. You can register your own action handlers written in JavaScript using the registerAction function. (See the send-email-action.js in the sample application for an example).

Custom actions can also be written in JavaScript.

import { NativeEventEmitter } from 'react-native'; import { RNAcousticMobilePushActionHandler } from 'NativeModules'; const emitter = new NativeEventEmitter(RNAcousticMobilePushActionHandler); export default function demonstrationAction(details) { // details.action contains the specific action payload ie {"type": "demonstration", "value": "acoustic"} // details.payload contains the entire push payload ie { "aps": { "alert": "test" }, "notification-action": {"type": "demonstration", "value": "acoustic"} } // You can place code here to use the values in those payloads to perform work on behalf of the user } // The registerAction call tells the SDK that you intend to handle actions of this type. In addition the function passed will be called for any missed actions received while your code was not running. RNAcousticMobilePushActionHandler.registerAction('demonstration', demonstrationAction); // The listener call allows this function to be called when actions arrive emitter.addListener('demonstration', demonstrationAction);

To include custom actions in your application, you must put them in a space-separated list in the custom actions key in the config.xml file. After that, you can register for callbacks to those actions through the MCEPlugin.setRegisteredActionCallback function. The callback includes both the action payload and the entire push payload for processing that action.

Register custom action callback

You have to register a callback with one or two arguments, the action, and the payload, for a specific custom action using the following:

MCEPlugin.setRegisteredActionCallback(callback, customActionName);

The following is an example of how to register the callback for the custom action:

MCEPlugin.setRegisteredActionCallback(function (action, payload) { const elem = document.getElementById('title'); const newText = action['value']; elem.innerText = newText; }, 'customUrl');

To create a custom action, you must pass user input through a register() function and send it to the SDK. The Flutter SDK then registers it as a custom action.

  1. Import the following packages.
import 'package:flutter/material.dart'; import 'package:flutter_acoustic_mobile_push/flutter_acoustic_sdk_push.dart';
  1. Create local variable:
var value = CustomActionValue();
  1. Add the registerCustomAction function to send the data to the SDK.
Future<void> registerCustomAction(String actionType) async { value.registerCustomAction(actionType); value.registeredEvent.subscribe((args) { var data = args!.changedValue; print('registering'); print(data); setState(() { statusValue = data; }); }); }
  1. The register function then passes the user input data to the registerCustomAction function.
register() { _yourFormKeyHere.currentState?.save(); if (_customActionType == null) { return Text(statusValue, style: TextStyle(color: labelColor)); } else { registerCustomAction("CoolAction"); } }

In the example above, we are creating and registering a custom action called "CoolAction".

  1. To unregister a custom action, add the unregister function. The unregister function then passes the user input data to the unregisterCustomAction function.
Future<void> unregisterCustomAction(String actionType) async { value.unregisterCustomAction(actionType); value.unregisiteredEvent.subscribe((args) { var data = args!.changedValue; setState(() { statusValue = data; }); }); }

📘

Note:

The register function must be tied to a register button or inkwell widget that is used to save the user input when the clicked or the tap action is performed.

For more information about registerCustomAction functionality, see
flutter_acoustic_sdk_push.dart

Override Actions

You can override actions and replace an old action with a new one. If you assigned a key to the actionType and did not use the default key to map the action to the SDK, you must first unregister the key and then assign a new one.

  1. Add the unregisterCustomAction function from the SDK.
Future<void> unregisterCustomAction(String actionType) async { value.unregisterCustomAction(actionType); value.unregisteredEvent.subscribe((args) { var data = args!.changedValue; setState(() { statusValue = data; }); }); }
  1. Add the unregister function.
unregister() { _yourFormKeyHere.currentState?.save(); unregisterCustomAction(_customActionType); }
  1. After unregistering the action type, you should now be able to register a new action type and override/update the key for the map (value) of the previous action.
    For more information, see