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 version | Android | API level |
|---|---|---|
| 1.0+ | 5.0 or later | 21+ |
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.
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
This quickstart uses a Gradle remote dependency. For all import options, see Import the SDK.
-
Configure the Maven repositories for your Gradle version.
For Gradle 8.0 or later, add the following to
settings.gradle:GroovydependencyResolutionManagement {
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:Groovyallprojects {
repositories {
mavenCentral()
maven { url "https://maven.nexconn.ai/repository/maven-releases/" }
}
} -
Add the Chat UI SDK dependency to your app-level
build.gradle. Replacex.y.zwith the latest version from the Maven repository.Groovydependencies {
implementation 'ai.nexconn.chat:chatui:x.y.z'
}Version pairingChat 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:chatexplicitly, or if you integrate local AAR files, use the same version forchatandchatui. 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.
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
- Java
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))
}
import ai.nexconn.chat.params.InitParams;
import ai.nexconn.chatui.NCChatUI;
@Override
public void onCreate() {
super.onCreate();
String appKey = "Your_AppKey";
NCChatUI.initialize(new InitParams(getApplicationContext(), appKey));
}
Global data center
If you use another data center, pass the corresponding AreaCode.
- Kotlin
- Java
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)
}
import ai.nexconn.chat.params.AreaCode;
import ai.nexconn.chat.params.InitParams;
import ai.nexconn.chatui.NCChatUI;
@Override
public void onCreate() {
super.onCreate();
String appKey = "Your_AppKey";
InitParams initParams = new InitParams(getApplicationContext(), appKey);
initParams.setAreaCode(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:
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.
The Nexconn SDK has a built-in reconnection mechanism. Call connect() only once per app lifecycle.
- Kotlin
- Java
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}")
}
}
})
import ai.nexconn.chat.params.ConnectParams;
import ai.nexconn.chat.handler.ConnectHandler;
import ai.nexconn.chat.error.NCError;
String token = "gxld6GHx3t1eDxof1qtxxYrQcjkbhl1V@sgyu.cn.example.com;sgyu.cn.example.com";
NCChatUI.connect(new ConnectParams(token), new ConnectHandler() {
@Override
public void onResult(String userId, NCError error) {
if (error == null && userId != null) {
System.out.println("Connected as user: " + userId);
} else {
System.out.println("Connection failed: " + (error != null ? error.getMessage() : ""));
}
}
@Override
public void onDatabaseOpened(boolean isRecreated, NCError error) {
if (error != null) {
System.out.println("Database open failed: " + error.getMessage());
}
}
});
Step 4: Open the channel list page
- Kotlin
- Java
val fragment = NCChatUI.getFragmentFactory().newChannelListFragment(Bundle())
supportFragmentManager
.beginTransaction()
.replace(R.id.container, fragment)
.commit()
Fragment fragment = NCChatUI.getFragmentFactory().newChannelListFragment(new Bundle());
getSupportFragmentManager()
.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
- Java
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()
Bundle args = new Bundle();
args.putInt("channelType", 1); // ChannelType.DIRECT value
args.putString("targetId", "2");
Fragment fragment = NCChatUI.getFragmentFactory().newChannelFragment(args);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, fragment)
.commit();