Edit in GitHubLog an issue

Execute JavaScript methods from native code

You can execute JavaScript in an in-app message from native code by completing the following steps:

Implement and assign a PresentationDelegate/ MessagingDelegate

To register a JavaScript event handler with a Message object, you will first need to implement and set a PresentationDelegate or MessagingDelegate (for older Android SDK versions and iOS SDK) you are using.

Please read the tutorial for more detailed instructions on implementing and using a PresentationDelegate/ MessagingDelegate (for older Android SDK versions and iOS SDK).

Obtain a reference to the InAppMessageEventHandler/ web view

In the shouldShowMessage function of the MessagingDelegate, get a reference to the web view used by the message.

In the onShow function of the PresentationDelegate, obtain a reference to the InAppMessageEventHandler for use in Javascript interactions.

Kotlin

Copied to your clipboard
var eventHandler: InAppMessageEventHandler? = null
var currentMessagePresentable: Presentable<InAppMessage>? = null
override fun onShow(presentable: Presentable<*>) {
if (presentable.getPresentation() !is InAppMessage) {
return
}
currentMessagePresentable = presentable as Presentable<InAppMessage>
eventHandler = currentMessagePresentable?.getPresentation()?.eventHandler
}

Java

Copied to your clipboard
InAppMessageEventHandler eventHandler = null;
Presentable<InAppMessage> currentMessagePresentable = null;
@Override
public void onShow(Presentable<?> presentable) {
if (!(presentable.getPresentation() instanceof InAppMessage)) {
return;
}
currentMessagePresentable = (Presentable<InAppMessage>) presentable;
eventHandler = currentMessagePresentable.getPresentation().getEventHandler();
}

Call the JavaScript method

With a reference to the InAppMessageEventHandler, the instance method evaluateJavascript(String, AdobeCallback<String>) can now be leveraged to call a JavaScript method.

Further details of this API are explained in the Android documentation - the example below is provided for the purpose of demonstration:

Kotlin

Copied to your clipboard
var eventHandler: InAppMessageEventHandler? = null
var currentMessagePresentable: Presentable<InAppMessage>? = null
override fun onShow(presentable: Presentable<*>) {
if (presentable.getPresentation() !is InAppMessage) {
return
}
currentMessagePresentable = presentable as Presentable<InAppMessage>
eventHandler = currentMessagePresentable?.getPresentation()?.eventHandler
eventHandler?.evaluateJavascript("startTimer()") { content ->
// do something with the content
}
}

Java

Copied to your clipboard
InAppMessageEventHandler eventHandler = null;
Presentable<InAppMessage> currentMessagePresentable = null;
@Override
public void onShow(Presentable<?> presentable) {
if (!(presentable.getPresentation() instanceof InAppMessage)) {
return;
}
currentMessagePresentable = (Presentable<InAppMessage>) presentable;
eventHandler = currentMessagePresentable.getPresentation().getEventHandler();
if (eventHandler != null) {
eventHandler.evaluateJavascript("startTimer()", s -> {
// do something with the content
});
}
}

Examples

The test apps in this repository demonstrate executing JavaScript code from an in-app message's webview:

  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.