iOS > Advanced

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...)

Note: On iOS, actions are mainly used for mobile landings, but their usage will be extended in future Batch versions.

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 block

Registering them is easy, and should be done every time your app starts, right before starting Batch:

BatchUserActionBlock alertActionBlock = ^(NSString * _Nonnull identifier, NSDictionary<NSString *,NSObject *> * _Nonnull arguments, id<BatchUserActionSource>  _Nullable source) {
    // Your action code here
};
[BatchActions registerAction:[BatchUserAction userActionWithIdentifier:@"<YOUR_ACTION_NAME>"
                                                           actionBlock:alertActionBlock]];
BatchActions.register(BatchUserAction(identifier: "<YOUR_ACTION_NAME>",
                                      actionBlock: { (identifier: String, arguments: [String : NSObject], source: BatchUserActionSource?) in
    // Your action code here
}))

The source argument represents what caused the action to be triggered. You can use isKindOfClass (or a swift cast using as?) to check what source it comes from, and read what you need.
Two sources classes are currently used: BatchPushMessage and BatchManualUserActionSource. More will be added in the future.

Unregistering an action

Simply call the unregister method with the previously set identifier:

[BatchActions unregisterActionIdentifier:@"<YOUR_ACTION_NAME>"];
BatchActions.unregister(actionIdentifier: "<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:

[BatchActions performActionIdentifiedBy:@"alert" withArguments:@{@"title":@"Hello", @"body":@"How are you doing?"}];
BatchActions.perform(actionIdentifier: "alert", arguments: ["title": "Hello", "body": "How are you doing?"])

Note: Built-in Batch actions are reserved to the SDK and not triggerable that way