Custom actions
Batch Actions is a module allowing you to register remotely-configurable runnable code to the SDK, when simple deeplinks wouldn't be enough. They can be triggered at any time by the SDK, allowing you to focus on the action code rather than when to trigger it.
Batch comes with builtin actions (deeplinking, user data edition, etc...)
Registering an action
An action has two components:
- An identifier (case-unsensitive string), which will allow you to reference this action
- While the identifier string is up to you, it cannot start with "batch.": these identifiers are reserved for built-in actions.
- An implementation class
Registering them is easy, and should be done in your Application subclass, right before starting Batch:
Batch.Actions.register(new UserAction("<YOUR_ACTION_NAME>", new UserActionRunnable() {
@Override
public void performAction(@Nullable Context context, @NonNull String identifier, @NonNull JSONObject args, @Nullable UserActionSource source) {
// Your action code here
}
}));
The source argument represents what caused the action to be triggered. You can use instanceof
to check what source it comes from, and read extra arguments (like the custom payload).
Two sources interfaces are currently used: InAppMessageUserActionSource
and PushUserActionSource
. More might be added in the future.
Unregistering an action
Simply call the unregister method with the previously set identifier:
Batch.Actions.unregister("<YOUR_ACTION_NAME>");
Triggering an action
You might want to re-use the code you've already registered to Batch from your own code by triggering an action.
Simply give Batch the action identifier and arguments, and it will call your action:
try {
final JSONObject args = new JSONObject();
args.put("title", "Hello");
args.put("body", "How are you doing?");
Batch.Actions.performAction(this, "alert", args);
} catch (JSONException e) {
e.printStackTrace();
}
Note: Built-in Batch actions are reserved to the SDK and not triggerable that way
Built-in actions
Batch provide a set of pre-defined actions, allowing you to easily trigger a specific behavior without implementing them yourself.
Identifier | Description | Arguments | |||
---|---|---|---|---|---|
batch.dismiss | Simply dismiss the notification and exit | N/A | |||
batch.deeplink | Open the provided deeplink (will call the DeeplinkInterceptor if one is set) | Object - Required | |||
l | String - Required The deeplink to open E.g. {"l":"https://google.com"} | ||||
li | Boolean - Optional - Default false If true the Batch SDK will try to open your deeplink inside the app using Chrome CustomTabs E.g. {"li":false} | ||||
batch.android_request_notifications | Show the notification permissions pop-up | N/A | |||
batch.android_redirect_settings | Open the notifications settings of the current application | N/A | |||
batch.android_smart_reoptin | Checks if the user has already been asked for notifications, if not it shows the notification permissions pop-up, otherwise it opens the notifications settings of the current application | N/A | |||
batch.user.tag | Add or remove a tag associated with the current user | Object - Required | |||
a | String - Required The action to perform. Acceptable values are add and remove E.g. {"a":"remove"} | ||||
c | String - Required The collection to use when updating the tag E.g. {"c":"actions"} | ||||
t | String - Required The tag to update E.g. {"t":"has_bought"} | ||||
batch.user.event | Send an event with associated tags and data | Object - Required | |||
e | String - Required The name of the event to send E.g. {"e":"my_awesome_event"} | ||||
l | String - Optional - default null The label associated with the event E.g. {"l":"label1"} | ||||
t | Array - Optional - default empty list A list of tags to be send with the event E.g. {"t":["tag1","tag2","tag3"]} | ||||
a | Object - Optional - default empty object The data to send with the event E.g. {"a":{"key":"value","key2":12,"key3":true}} | ||||
batch.group | Execute a list of up to 10 nested actions successively | Object - Required | |||
actions | Array - Required A list of actions to perform. This list must not be longer than 10 elements and can't contains batch.group actions E.g. {"actions":[["batch.deeplink",{"l":"https://google.com"}],["batch.user.tag",{"add":"..."}]]} | ||||
batch.clipboard | Copy a text to the device's clipboard | Object - Required | |||
t | String - Required The text to copy E.g. {"t":"My awesome text !"} | ||||
d | String - Optional - default "text" A descriptive label for the text E.g. {"d":"Phone number"} | ||||
batch.rating | Opens the Google Play In-App Review prompt and asks the user to review the app. Falls back on opening the Play Store or Huawei AppGallery if the Play Core library is not integrated or errors out. Available since Batch 1.17. | N/A |