javascriptandroidhtmlurbanairship.com

UrbanAirship - HTML Button - set tag to pass to code


What I am wanting to do is simple. I am using UrbanAirship to show a modal view ("In-app Automation"), which is HTML. I have a button in my HTML. The message/view is showing fine when the event is fired. Right now I have in the HTML for the button:

<button class="light" type="button" onclick="UAirship.close()">OK</button>

When the button is tapped, the call to UAirship.close() works fine and closes the message. I setup a listener in my Android code, such as follows:

UAirship.shared()
            .getInAppMessagingManager()
            .addListener(new InAppMessageListener() {
                @Override
                public void onMessageDisplayed(@NonNull String scheduleId, @NonNull InAppMessage message) {
                    // gets here fine
                }

                @Override
                public void onMessageFinished(@NonNull String scheduleId, @NonNull InAppMessage message, @NonNull ResolutionInfo resolutionInfo) {
                    // gets here fine as well
                }
            });

It gets into those methods without any issues when the message/view shows and dismisses. However, I want to be able to detect one particular button was tapped vs. others I have in the HTML. Is there a way to go about this easily, so that some custom tag or something can be passed in the "ResolutionInfo" object or "InAppMessage"?

My hopes were that there would be something like "UAirship.close('tag')", where that tag would be passed back. Or even a custom HTML tag I can put on the button that would be recognized in the code. Really just any way of going about it is fine, I just can't seem to find in the documentation how to properly do this with HTML.

I'm looking at the documentation here. I see the UAirship.runAction() method, but it's not really clear to me what a simple implementation would be for this (also, the "CustomAction" class referenced in the action registering doesn't even exist in the library it seems..).

I basically just need to call one line of code in my Activity when the Airship message closes after that button is tapped.

Thoughts?


Solution

  • I've found that this solution works, although there may be a better one out there. In the HTML, for the button, call the close() and then a runAction() call:

    onclick="UAirship.close(); UAirship.runAction('my_action', null, null)"
    

    Then in the code, in my onCreate() in my Activity, I have this:

    final String actionName = "my_action";
    Action customAction = new Action() {
        @NonNull
        @Override
        public ActionResult perform(@NonNull ActionArguments actionArguments) {
            String buttonActionName = actionArguments.getMetadata().getString(ActionArguments.REGISTRY_ACTION_NAME_METADATA);
            if (buttonActionName.equals(actionName)) {
                // DO WHAT IS NEEDED HERE
            }
            return ActionResult.newEmptyResult();
        }
    };
    ā€‹
    UAirship.shared()
        .getActionRegistry()
        .registerAction(customAction, actionName);