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
- Obtain a reference to the web view
- Call the JavaScript method
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 clipboardvar eventHandler: InAppMessageEventHandler? = nullvar currentMessagePresentable: Presentable<InAppMessage>? = nulloverride fun onShow(presentable: Presentable<*>) {if (presentable.getPresentation() !is InAppMessage) {return}currentMessagePresentable = presentable as Presentable<InAppMessage>eventHandler = currentMessagePresentable?.getPresentation()?.eventHandler}
Java
Copied to your clipboardInAppMessageEventHandler eventHandler = null;Presentable<InAppMessage> currentMessagePresentable = null;@Overridepublic void onShow(Presentable<?> presentable) {if (!(presentable.getPresentation() instanceof InAppMessage)) {return;}currentMessagePresentable = (Presentable<InAppMessage>) presentable;eventHandler = currentMessagePresentable.getPresentation().getEventHandler();}
On Android, the web view is represented as WebView
.
Java
Copied to your clipboard@Overridepublic boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {// access to the whole message from the parentMessage message = (Message) fullscreenMessage.getParent();WebView webView = message.getWebView();...}
On iOS, the web view is represented as WKWebView
.
Swift
Copied to your clipboardfunc shouldShowMessage(message: Showable) -> Bool {// access to the whole message from the parentlet fullscreenMessage = message as? FullscreenMessagelet message = fullscreenMessage?.parentlet messageWebView = message?.view as? WKWebView...}
In the onShow
function of the PresentationDelegate
, obtain a reference to the InAppMessageEventHandler
for use in Javascript interactions.
Kotlin
Copied to your clipboardvar eventHandler: InAppMessageEventHandler? = nullvar currentMessagePresentable: Presentable<InAppMessage>? = nulloverride fun onShow(presentable: Presentable<*>) {if (presentable.getPresentation() !is InAppMessage) {return}currentMessagePresentable = presentable as Presentable<InAppMessage>eventHandler = currentMessagePresentable?.getPresentation()?.eventHandler}
Java
Copied to your clipboardInAppMessageEventHandler eventHandler = null;Presentable<InAppMessage> currentMessagePresentable = null;@Overridepublic void onShow(Presentable<?> presentable) {if (!(presentable.getPresentation() instanceof InAppMessage)) {return;}currentMessagePresentable = (Presentable<InAppMessage>) presentable;eventHandler = currentMessagePresentable.getPresentation().getEventHandler();}
On Android, the web view is represented as WebView
.
Java
Copied to your clipboard@Overridepublic boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {// access to the whole message from the parentMessage message = (Message) fullscreenMessage.getParent();WebView webView = message.getWebView();...}
On iOS, the web view is represented as WKWebView
.
Swift
Copied to your clipboardfunc shouldShowMessage(message: Showable) -> Bool {// access to the whole message from the parentlet fullscreenMessage = message as? FullscreenMessagelet message = fullscreenMessage?.parentlet messageWebView = message?.view as? WKWebView...}
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 clipboardvar eventHandler: InAppMessageEventHandler? = nullvar currentMessagePresentable: Presentable<InAppMessage>? = nulloverride fun onShow(presentable: Presentable<*>) {if (presentable.getPresentation() !is InAppMessage) {return}currentMessagePresentable = presentable as Presentable<InAppMessage>eventHandler = currentMessagePresentable?.getPresentation()?.eventHandlereventHandler?.evaluateJavascript("startTimer()") { content ->// do something with the content}}
Java
Copied to your clipboardInAppMessageEventHandler eventHandler = null;Presentable<InAppMessage> currentMessagePresentable = null;@Overridepublic 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});}}
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@Overridepublic boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {// access to the whole message from the parentMessage message = (Message) fullscreenMessage.getParent();WebView webView = message.getWebView();// webview operations must be run on the ui threadwebView.post(new Runnable() {@Overridepublic void run() {webView.evaluateJavascript("startTimer()", new ValueCallback<String>() {@Overridepublic void onReceiveValue(String s) {// do something with the content}});}});...}
With a reference to the WKWebView
, the instance method evaluateJavaScript(_:completionHandler:)
can now be leveraged to call a JavaScript method.
Further details of this API are explained in the Apple documentation - the example below is provided for the purpose of demonstration:
Swift
Copied to your clipboardfunc shouldShowMessage(message: Showable) -> Bool {// access to the whole message from the parentlet fullscreenMessage = message as? FullscreenMessagelet message = fullscreenMessage?.parent// the `shouldShowMessage` delegate method is called on a background thread.// need to dispatch code that uses the webview back to the main thread.DispatchQueue.main.async {let messageWebView = message?.view as? WKWebViewmessageWebView?.evaluateJavaScript("startTimer();") { result, error inif error != nil {// handle errorreturn}if result != nil {// do something with the result}}}...}
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 clipboardvar eventHandler: InAppMessageEventHandler? = nullvar currentMessagePresentable: Presentable<InAppMessage>? = nulloverride fun onShow(presentable: Presentable<*>) {if (presentable.getPresentation() !is InAppMessage) {return}currentMessagePresentable = presentable as Presentable<InAppMessage>eventHandler = currentMessagePresentable?.getPresentation()?.eventHandlereventHandler?.evaluateJavascript("startTimer()") { content ->// do something with the content}}
Java
Copied to your clipboardInAppMessageEventHandler eventHandler = null;Presentable<InAppMessage> currentMessagePresentable = null;@Overridepublic 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});}}
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@Overridepublic boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {// access to the whole message from the parentMessage message = (Message) fullscreenMessage.getParent();WebView webView = message.getWebView();// webview operations must be run on the ui threadwebView.post(new Runnable() {@Overridepublic void run() {webView.evaluateJavascript("startTimer()", new ValueCallback<String>() {@Overridepublic void onReceiveValue(String s) {// do something with the content}});}});...}
With a reference to the WKWebView
, the instance method evaluateJavaScript(_:completionHandler:)
can now be leveraged to call a JavaScript method.
Further details of this API are explained in the Apple documentation - the example below is provided for the purpose of demonstration:
Swift
Copied to your clipboardfunc shouldShowMessage(message: Showable) -> Bool {// access to the whole message from the parentlet fullscreenMessage = message as? FullscreenMessagelet message = fullscreenMessage?.parent// the `shouldShowMessage` delegate method is called on a background thread.// need to dispatch code that uses the webview back to the main thread.DispatchQueue.main.async {let messageWebView = message?.view as? WKWebViewmessageWebView?.evaluateJavaScript("startTimer();") { result, error inif error != nil {// handle errorreturn}if result != nil {// do something with the result}}}...}
Examples
The test apps in this repository demonstrate executing JavaScript code from an in-app message's webview: