> For the complete documentation index, see [llms.txt](https://partner-docs.covergenius.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://partner-docs.covergenius.com/xcover-journeys/integration/messaging/android.md).

# Android

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

{% hint style="info" %}
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](https://developer.android.com/reference/android/webkit/WebView) and [addJavascriptInterface](https://developer.android.com/reference/android/webkit/WebView#addJavascriptInterface\(java.lang.Object,%20java.lang.String\)).
{% endhint %}

### 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:

```kotlin
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:

```kotlin
fun sendToJourney(webView: WebView, type: String, payload: JSONObject = JSONObject()) {
    val js = """
        window.postMessage({ source: 'xcover-journeys', type: '$type', payload: ${payload} }, '*');
    """.trimIndent()
    webView.evaluateJavascript(js, null)
}

// Example: ask the journey to hide itself
sendToJourney(webView, "CG_JOURNEY_HIDE")
```

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://partner-docs.covergenius.com/xcover-journeys/integration/messaging/android.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
