AI Integration Quick Reference
AI Integration Quick Reference
| Field | Value |
|---|---|
| Kotlin (XML Views) | com.cometchat:chat-uikit-kotlin |
| Jetpack Compose | com.cometchat:chat-uikit-compose |
| Import | com.cometchat.uikit.core.CometChatUIKit |
| Init | CometChatUIKit.init(context, uiKitSettings, callback) |
| Login (dev) | CometChatUIKit.login("UID", callback) |
| Login (prod) | CometChatUIKit.loginWithAuthToken("AUTH_TOKEN", callback) |
| Other methods | CometChatUIKit.logout(), CometChatUIKit.getLoggedInUser(), CometChatUIKit.createUser(user, callback) |
| Send messages | CometChatUIKit.sendTextMessage(), CometChatUIKit.sendMediaMessage(), CometChatUIKit.sendCustomMessage() |
| Note | Use these wrapper methods instead of raw SDK calls — they manage internal UI Kit eventing |
CometChatUIKit provides wrapper methods around the CometChat SDK for initialization, authentication, user creation, date formatting, and sending messages — while automatically managing internal UI Kit events so that components like Message List and Conversations stay in sync.
When to use this
- You need to initialize the CometChat UI Kit and SDK before rendering any UI components.
- You need to log a user in or out of CometChat using an Auth Key (development) or Auth Token (production).
- You need to create a new CometChat user dynamically from your app.
- You need to send text, media, or custom messages through the UI Kit so that the Message List and Conversations components update automatically.
- You need to customize how dates and times are displayed across all UI Kit components.
Prerequisites
- The
com.cometchat:chat-uikit-kotlinorcom.cometchat:chat-uikit-composedependency added to your project. - Your CometChat App ID, Region, and Auth Key (or Auth Token) from the CometChat dashboard.
CometChatUIKit.init()called before invoking any other UI Kit method.
API reference
Initialization
Init
You must invoke this method before using any other methods provided by the UI Kit. This initialization ensures the UI Kit and Chat SDK function correctly in your application. Call this as one of the first lines of code in your application’s lifecycle. Signature:What this does: Accepts aTheContext, aUIKitSettingsconfiguration object, and aCallbackListenerthat reports success or failure.
UIKitSettings is an important parameter of the init() function. It serves as the base settings object, housing properties such as appId, region, and authKey.
| Method | Type | Description |
|---|---|---|
| setAppId | String | Sets the unique ID for the app, available on dashboard |
| setRegion | String | Sets the region for the app (‘us’ or ‘eu’) |
| setAuthKey | String | Sets the auth key for the app, available on dashboard |
| subscribePresenceForAllUsers | String | Sets subscription type for tracking the presence of all users |
| subscribePresenceForFriends | String | Sets subscription type for tracking the presence of friends |
| subscribePresenceForRoles | String | Sets subscription type for tracking the presence of users with specified roles |
| setAutoEstablishSocketConnection | Boolean | Configures if web socket connections will established automatically on app initialization or be done manually, set to true by default |
| setAIFeatures | List<AIExtensionDataSource> | Sets the AI Features that need to be added in UI Kit |
| setExtensions | List<ExtensionsDataSource> | Sets the list of extension that need to be added in UI Kit |
| dateTimeFormatterCallback | DateTimeFormatterCallback | Interface containing callback methods to format different types of timestamps. |
What this does: Creates aUIKitSettingsobject with your app ID, region, and auth key, then initializes the CometChat UI Kit. On success, the SDK and UI Kit are ready for use. On error, theCometChatExceptionprovides failure details.
Authentication
Login using Auth Key
Only theUID of a user is needed to log in. This simple authentication procedure is useful when you are creating a POC or if you are in the development phase. For production apps, use Auth Token instead of Auth Key.
Signature:
What this does: Accepts a user ID string and aUsage:CallbackListenerthat returns the logged-inUserobject on success.
What this does: Logs in a user by theirUIDusing the Auth Key configured during initialization. On success, theUserobject is returned.
Login using Auth Token
This advanced authentication procedure does not use the Auth Key directly in your client code thus ensuring safety.- Create a User via the CometChat API when the user signs up in your app.
- Create an Auth Token via the CometChat API for the new user and save the token in your database.
- Load the Auth Token in your client and pass it to the
loginWithAuthToken()method.
What this does: Logs in a user using a server-generated Auth Token instead of an Auth Key. This is the recommended approach for production apps because the Auth Key never appears in client code.
Logout
The CometChat UI Kit and Chat SDK effectively handle the session of the logged-in user within the framework. Before a new user logs in, it is crucial to clean this data to avoid potential conflicts or unexpected behavior. This can be achieved by invoking thelogout() function.
Signature:
What this does: Logs out the current user, clears the session data, and tears down the internal event system. Call this before logging in a different user to avoid conflicts.
Create User
You can dynamically create users on CometChat using thecreateUser() function. This is useful when users are registered or authenticated by your system and then need to be created on CometChat.
Signature:
What this does: Creates a new user on CometChat with the specified UID, name, and avatar URL. On success, the created User object is returned.
DateFormatter
By providing a custom implementation of theDateTimeFormatterCallback, you can globally configure how time and date values are displayed across all UI components in the CometChat UI Kit. This ensures consistent formatting for labels such as “Today”, “Yesterday”, “X minutes ago”, and more, throughout the entire application.
Each method in the interface corresponds to a specific case:
time(long timestamp) → Custom full timestamp format
today(long timestamp) → Called when a message is from today
yesterday(long timestamp) → Called for yesterday’s messages
lastWeek(long timestamp) → Messages from the past week
otherDays(long timestamp) → Older messages
minute(long timestamp) / hour(long timestamp) → Exact time unit
minutes(long diffInMinutesFromNow, long timestamp) → e.g., “5 minutes ago”
hours(long diffInHourFromNow, long timestamp) → e.g., “2 hours ago”
Usage:
What this does: Configures a customDateTimeFormatterCallbackonUIKitSettingsand passes it toCometChatUIKit.init(). This globally overrides how timestamps appear across all UI Kit components.
Base Message
Text Message
To send a text message to a single user or a group, use thesendTextMessage() function. This function requires a TextMessage object as its argument, which contains the necessary information for delivering the message.
It’s essential to understand the difference betweenSignature:CometChatUIKit.sendTextMessage()andCometChat.sendTextMessage(). When you useCometChatUIKit.sendTextMessage(), it automatically adds the message to the Message List and Conversations components, taking care of all related cases for you. On the other hand,CometChat.sendTextMessage()only sends the message and doesn’t automatically update these components in the UI Kit.
What this does: Creates aTextMessagetargeting a user with the receiver typeRECEIVER_TYPE_USER, then sends it through the UI Kit. The message automatically appears in the Message List and Conversations components.
Media Message
To send a media message to a single user or a group, use thesendMediaMessage() function. This function requires a MediaMessage object as its argument.
When you useSignature:CometChatUIKit.sendMediaMessage(), it automatically adds the message to the Message List and Conversations components.CometChat.sendMediaMessage()only sends the message without updating UI Kit components.
What this does: Creates aMediaMessagewith a file path and message typeMESSAGE_TYPE_FILE, then sends it through the UI Kit.
Custom Message
To send a custom message to a single user or a group, use thesendCustomMessage() function. This function requires a CustomMessage object as its argument.
When you useSignature:CometChatUIKit.sendCustomMessage(), it automatically adds the message to the Message List and Conversations components.CometChat.sendCustomMessage()only sends the message without updating UI Kit components.
What this does: Creates a CustomMessage with a custom type string and JSON data payload, then sends it through the UI Kit.
Note: To display custom messages in the MessageList, you must create and register a new MessageTemplate that defines how to render your custom message type.
Next steps
Events Reference
Listen for real-time UI Kit events for users, groups, conversations, messages, and calls
Message List
Display and customize the message list where sent messages appear
Conversations
Display and customize the conversations list that updates when messages are sent