Native handling of JavaScript events
iOS
Android
You can handle events from in-app message interactions natively within your application by completing the following steps:

Implement and assign a MessagingDelegate

To register a JavaScript event handler with a Message object, you will first need to implement and set a MessagingDelegate.
For more detailed instructions on implementing and using a MessagingDelegate, please read the tutorial on using MessagingDelegate.

Register a JavaScript handler for your In-App Message

In the shouldShowMessage function of the MessagingDelegate, call handleJavascriptMessage(_:withHandler) to register your handler.
The name of the message you intend to pass from the JavaScript side should be specified in the first parameter.
The following example shows a handler that dispatches an inapp.interact Experience Event natively when the JavaScript of the in-app message posts a myInappCallback message:
Swift
1
func shouldShowMessage(message: Showable) -> Bool {
2
let fullscreenMessage = message as? FullscreenMessage
3
let message = fullscreenMessage?.parent
4
5
// in-line handling of JavaScript calls
6
message?.handleJavascriptMessage("myInappCallback") { content
7
8
print("JavaScript body passed to native callback: \(content ?? "empty")")
9
10
message?.track(content as? String, withEdgeEventType: .inappInteract)
11
}
12
13
return true
14
}
Copied!
You can handle events from in-app message interactions natively within your application by completing the following steps:

Implement and assign a MessagingDelegate

To register a JavaScript event handler with a Message object, you will first need to implement and set a MessagingDelegate.
For more detailed instructions on implementing and using a MessagingDelegate, please read the tutorial on using MessagingDelegate.

Register a JavaScript handler for your In-App Message

In the shouldShowMessage function of the MessagingDelegate, call public void handleJavascriptMessage(final String name, final AdobeCallback<String> callback) to register your handler.
The name of the message you intend to pass from the JavaScript side should be specified in the first parameter.
The following example shows a handler that dispatches an inapp.interact Experience Event natively when the JavaScript of the in-app message posts a myInappCallback message:
Java
1
@Override
2
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
3
Message message = (Message) fullscreenMessage.getParent();
4
5
// in-line handling of JavaScript calls
6
message.handleJavascriptMessage("myInappCallback", new AdobeCallback<String>() {
7
@Override
8
public void call(String content) {
9
System.out.println("JavaScript body passed to native callback: " + content);
10
message.track(content, MessagingEdgeEventType.IN_APP_INTERACT);
11
}
12
});
13
}
Copied!

Post the JavaScript message from your In-App Message

Now that the in-app message has been displayed, the final step is to post the JavaScript message.
Continuing from the previous example, the developer is going to post the myInappCallback message from their HTML, which will in turn call the handler previously configured:

HTML

1
<html>
2
<head>
3
<script type="text/javascript">
4
function callNative(action) {
5
try {
6
// the name of the message handler is the same name that must be registered in native code.
7
// in this case the message name is "myInappCallback"
8
webkit.messageHandlers.myInappCallback.postMessage(action);
9
} catch(err) {
10
console.log('The native context does not exist yet'); }
11
}
12
</script>
13
</head>
14
<body>
15
<button onclick="callNative('native callbacks are cool!')">Native callback!</button>
16
</body>
17
</html>
Copied!
(The above HTML is not representative of a valid in-app message, and is intended only to demonstrate how to post the JavaScript message).
When the user clicks the button inside of this in-app message, the handler configured in the previous step will be called. The handler will send an Experience Event tracking the interaction, and print the following message to the console:
1
JavaScript body passed to native callback: native callbacks are cool!
Copied!
Export as PDF
Copy link
Edit on GitHub