Skip to main content

Send your first message

This quickstart walks you through the core Nexconn Chat UI flow: import the SDK, initialize it, connect to the server, and open a channel page.

Requirements

SDK versionAndroidAPI level
1.0+5.0 or later21+

Prerequisites

  • Register a developer account on the Nexconn Console. A default application is automatically created in the development environment upon registration.
  • On the Key Management page, obtain the App Key for your development environment. You can also view the App Secret and data center assignment (Singapore by default).
  • Optionally, create a new application and obtain the App Key and App Secret for the desired environment.
tip

Each application has two separate App Keys for the development and production environments. Data is isolated between environments. Before going live, switch to the production App Key to complete end-to-end testing.

Step 1: Import the SDK

info

This quickstart uses a Gradle remote dependency. For all import options, see Import the SDK.

  1. Configure the Maven repositories for your Gradle version.

    For Gradle 8.0 or later, add the following to settings.gradle:

    Groovy
    dependencyResolutionManagement {
    repositories {
    mavenCentral()
    maven { url "https://maven.nexconn.ai/repository/maven-releases/" }
    }
    }

    For Gradle versions earlier than 8.0, add the following to the root build.gradle:

    Groovy
    allprojects {
    repositories {
    mavenCentral()
    maven { url "https://maven.nexconn.ai/repository/maven-releases/" }
    }
    }
  2. Add the Chat UI SDK dependency to your app-level build.gradle. Replace x.y.z with the latest version from the Maven repository.

    Groovy
    dependencies {
    implementation 'ai.nexconn.chat:chatui:x.y.z'
    }
    Version pairing

    Chat UI depends on Chat SDK. When you use the Maven dependency, Gradle resolves the required Chat SDK transitively. If you also declare ai.nexconn.chat:chat explicitly, or if you integrate local AAR files, use the same version for chat and chatui. Do not mix Chat SDK and Chat UI SDK versions from different releases.

Step 2: Initialize the SDK

Call NCChatUI.initialize() in your Application's onCreate() method. Obtain your App Key from the Nexconn Console before proceeding.

info

This quickstart shows basic initialization. For full options including overseas data centers and private cloud configuration, see Initialization.

Singapore data center (default)

If your App Key belongs to the Singapore data center, you do not need extra configuration.

kotlin
import ai.nexconn.chat.params.InitParams
import ai.nexconn.chatui.NCChatUI

override fun onCreate() {
super.onCreate()
val appKey = "Your_AppKey"
NCChatUI.initialize(InitParams(applicationContext, appKey))
}

Global data center

If you use another data center, pass the corresponding AreaCode.

kotlin
import ai.nexconn.chat.params.AreaCode
import ai.nexconn.chat.params.InitParams
import ai.nexconn.chatui.NCChatUI

override fun onCreate() {
super.onCreate()
val appKey = "Your_AppKey"
val initParams = InitParams(applicationContext, appKey).apply {
areaCode = AreaCode.SG
}
NCChatUI.initialize(initParams)
}

Step 3: Connect to the server

Get an access token

To simulate messaging, register a user first. In production, your app server calls the Nexconn Server API to register a user and obtain an access token. For this quickstart, use the API debugging tool in the Console to generate a token for user ID 1:

JSON
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
"code": 200,
"userId": "1",
"token": "gxld6GHx3t1eDxof1qtxxYrQcjkbhl1V@sgyu.cn.example.com;sgyu.cn.example.com"
}

Connect

Call NCChatUI.connect() with the access token to establish a connection.

tip

The Nexconn SDK has a built-in reconnection mechanism. Call connect() only once per app lifecycle.

kotlin
import ai.nexconn.chat.error.NCError
import ai.nexconn.chat.handler.ConnectHandler
import ai.nexconn.chat.params.ConnectParams

val token = "gxld6GHx3t1eDxof1qtxxYrQcjkbhl1V@sgyu.cn.example.com;sgyu.cn.example.com"
NCChatUI.connect(ConnectParams(token), object : ConnectHandler {
override fun onResult(userId: String?, error: NCError?) {
if (error == null && userId != null) {
println("Connected as user: $userId")
} else {
println("Connection failed: ${error?.message.orEmpty()}")
}
}

override fun onDatabaseOpened(isRecreated: Boolean, error: NCError?) {
if (error != null) {
println("Database open failed: ${error.message}")
}
}
})

Step 4: Open the channel list page

kotlin
val fragment = NCChatUI.getFragmentFactory().newChannelListFragment(Bundle())
supportFragmentManager
.beginTransaction()
.replace(R.id.container, fragment)
.commit()

Step 5: Open a channel page

Open a direct channel with user ID 2. You can use this page to send your first message through the Chat UI.

kotlin
val args = Bundle().apply {
putInt("channelType", 1) // ChannelType.DIRECT value
putString("targetId", "2")
}

val fragment = NCChatUI.getFragmentFactory().newChannelFragment(args)
supportFragmentManager
.beginTransaction()
.replace(R.id.container, fragment)
.commit()

What's next