Android
Send and receive journey events through a WebView bridge on Android.
Prerequisites
The journey is loaded inside a WebView with JavaScript enabled (webView.settings.javaScriptEnabled = true).
Receiving events from the journey
Expose a @JavascriptInterface bridge object and inject a script that forwards window.postMessage calls into it:
class JourneyBridge(private val onEvent: (String, JSONObject) -> Unit) {
@JavascriptInterface
fun postMessage(json: String) {
val data = JSONObject(json)
if (data.optString("source") != "xcover-journeys") return
val type = data.optString("type")
val payload = data.optJSONObject("payload") ?: JSONObject()
onEvent(type, payload)
}
}
// Setup
webView.settings.javaScriptEnabled = true
webView.addJavascriptInterface(
JourneyBridge { type, payload ->
when (type) {
"CG_JOURNEY_READY" -> Log.d("XCJ", "Journey mounted and ready")
"CG_JOURNEY_COMPLETE" -> Log.d(
"XCJ",
"Journey finished: ${payload.optString("outcome")} ${payload.optString("policy_id")}"
)
}
},
"cgJourneyBridge"
)
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
val bridgeScript = """
window.addEventListener('message', function(event) {
if (event.data && event.data.source === 'xcover-journeys') {
cgJourneyBridge.postMessage(JSON.stringify(event.data));
}
});
""".trimIndent()
view.evaluateJavascript(bridgeScript, null)
}
}
webView.loadUrl("https://xcj.xcover.com/acme-travel/en/?country=AU")Sending events into the journey
Evaluate JavaScript in the web view to dispatch a message event the journey's listener picks up:
Origin and security notes
The journey's own trusted-origin validation is designed for browser postMessage traffic (checked against event.origin). Inside a native WebView bridge, the risk model shifts instead to this: only your app's injected bridge script can call cgJourneyBridge.postMessage, and only code running inside the loaded page (the journey itself) can trigger it
Load only the journey URL provided for your integration.
Do not attach this bridge to a web view that can load arbitrary or user-supplied URLs, since addJavascriptInterface exposes the bridge to any page the view loads.
Last updated
Was this helpful?

