For the complete documentation index, see llms.txt. This page is also available as Markdown.

Android

Send and receive journey events through a WebView bridge on Android.

There is no separate native SDK or bridge shipped by Cover Genius for Android. You relay the same JSON envelope described on the main Messaging page through WebView's standard @JavascriptInterface mechanism, exactly as you would for any other web content.

This page shows the CG specific envelope and events; for the underlying bridge mechanism itself, see Android's official documentation for WebView and addJavascriptInterface.

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?