Adding Decagon to your Android App

There are plenty of resources online about adding a WebView to Android so this guide will focus primarily on what to put in your WebView!

Step 1: Add the Webview to your Layout

Go to your app's layout file (e.g., activity_main.xml) in the res/layout folder. Then, add the WebView Widget. For example:

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Step 2: Configure your Activity

Open the Java/Kotlin Activity File: For example, MainActivity.java or MainActivity.kt Then, initialize WebView:

  • Java:

javaCopy codeWebView myWebView = (WebView) findViewById(R.id.webview);
  • In Kotlin:

kotlinCopy codeval myWebView: WebView = findViewById(R.id.webview)

Finally, point the Webview to your unique URL that contains your mobile chat AI

myWebView.loadUrl("https://decagon.ai/mobile/<your company>");

Step 3: Pass in Metadata

You may pass in arbitrary, unique information about the user as metadata by adding URL params to the URL. For example:

myWebView.loadUrl("https://decagon.ai/mobile/<your company>?userId=...&email=...");

That's it! You may find more in-depth guides on adding WebViews in Android on the internet if you need further assistance.

Step 4: (Optional) Pass in user token

If a token needs to used by the AI to uniquely and securely identify a user (e.g. to use for API calls), we recommend passing it in via the X-DECAGON-USER-TOKEN URL header in your WebView, and not as an URL param. This token will be passed to the Decagon backend on every chat message to be used in AI workflows.

Create a Map<String, String> (i.e. a headers map) that will hold your custom headers. For example:

Map<String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("X-DECAGON-USER-TOKEN", "<your_token_for_the_user>");

Then, include the headers map when loading your WebView.

myWebView.loadUrl(
    "https://decagon.ai/mobile/<your company>?userId=...&email=...",
    extraHeaders
);

See here for more info.

Last updated