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

Obtain a reference to the web view

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

On Android, the web view is represented as WebView.

Java

Copied to your clipboard
@Override
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
// access to the whole message from the parent
Message message = (Message) fullscreenMessage.getParent();
WebView webView = message.getWebView();
...
}

Call the JavaScript method

With a reference to the WebView, the instance method public void evaluateJavascript(@NonNull String script, @Nullable ValueCallback<String> resultCallback) 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:

Java

Copied to your clipboard
@Override
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
// access to the whole message from the parent
Message message = (Message) fullscreenMessage.getParent();
WebView webView = message.getWebView();
// webview operations must be run on the ui thread
webView.post(new Runnable() {
@Override
public void run() {
webView.evaluateJavascript("startTimer()", new ValueCallback<String>() {
@Override
public void onReceiveValue(String 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 © 2025 Adobe. All rights reserved.