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, custom, or interactive 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
cometchat-chat-uikit-androiddependency 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.Make sure you replace the
APP_ID, REGION and AUTH_KEY with your CometChat App ID, Region and Auth Key in the below code. The Auth Key is an optional property of the UIKitSettings Class. It is intended for use primarily during proof-of-concept (POC) development or in the early stages of application development. You can use the Auth Token to log in securely.- Kotlin
- Java
What this does: Declares theTheinit()method signature. It accepts aContext, 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.
Here’s the table format for the properties available in UIKitSettings:
| 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. |
- Kotlin
- Java
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:
- Kotlin
- Java
What this does: Declares theUsage:login()method signature. It accepts a user ID string and aCallbackListenerthat returns the logged-inUserobject on success.
- Kotlin
- Java
What this does: Logs in a user by theirUIDusing the Auth Key configured during initialization. On success, theUserobject is returned. On error, theCometChatExceptionprovides failure details.
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.
- Kotlin
- Java
What this does: Declares theUsage:loginWithAuthToken()method signature. It accepts a server-generated auth token string and aCallbackListenerthat returns the logged-inUserobject on success.
- Kotlin
- Java
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. On success, the User object is returned.
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:
- Kotlin
- Java
What this does: Declares theUsage:logout()method signature. It accepts aCallbackListenerthat reports success or failure of the logout operation.
- Kotlin
- Java
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:
- Kotlin
- Java
What this does: Declares theUsage:createUser()method signature. It accepts aUserobject (with UID, name, and optional avatar) and aCallbackListenerthat returns the createdUseron success.
- Kotlin
- Java
What this does: Creates a new user on CometChat with the specified UID, name, and avatar URL. On success, the createdUserobject is returned. On error, theCometChatExceptionprovides failure details.
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:
- Kotlin
- Java
What this does: Configures a customDateTimeFormatterCallbackonUIKitSettingsand passes it toCometChatUIKit.init(). This globally overrides how timestamps appear across all UI Kit components — today’s messages show “Today”, yesterday’s show “Yesterday”, recent messages show “X mins ago” or “X hrs ago”, last week’s show “Last Week”, and older messages show the full date in “dd MMM yyyy” format.
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 MessagesListComponent and ConversationsComponent, 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.
- Kotlin
- Java
What this does: Declares theUsage:sendTextMessage()method signature. It accepts aTextMessageobject and aCallbackListenerthat returns the sentTextMessageon success.
- Kotlin
- Java
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, which contains the necessary information for delivering the message.
It’s essential to understand the difference betweenSignature:CometChatUIKit.sendMediaMessage()andCometChat.sendMediaMessage(). When you useCometChatUIKit.sendMediaMessage(), it automatically adds the message to the MessagesListComponent and ConversationsComponent, taking care of all related cases for you. On the other hand,CometChat.sendMediaMessage()only sends the message and doesn’t automatically update these components in the UI Kit.
- Kotlin
- Java
What this does: Declares theUsage:sendMediaMessage()method signature. It accepts aMediaMessageobject and aCallbackListenerthat returns the sentMediaMessageon success.
- Kotlin
- Java
What this does: Creates aMediaMessagewith a file path and message typeMESSAGE_TYPE_FILE, then sends it through the UI Kit. The message automatically appears in the Message List and Conversations components.
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, which contains the necessary information for delivering the message.
It’s essential to understand the difference betweenSignature:CometChatUIKit.sendCustomMessage()andCometChat.sendCustomMessage(). When you useCometChatUIKit.sendCustomMessage(), it automatically adds the message to the MessagesList and ConversationsComponent, taking care of all related cases for you. On the other hand,CometChat.sendCustomMessage()only sends the message and doesn’t automatically update these components in the UI Kit.
- Kotlin
- Java
What this does: Declares theUsage:sendCustomMessage()method signature. It accepts aCustomMessageobject and aCallbackListenerthat returns the sentCustomMessageon success.
- Kotlin
- Java
What this does: Creates a CustomMessage with a custom type string and JSON data payload, then sends it through the UI Kit. The message automatically appears in the Message List and Conversations components.
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.
Interactive Message
Form Message
To send a Form message to a single user or a group, utilize thesendFormMessage() function. This function requires a FormMessage object as its argument, which contains the necessary information to create a form bubble for that message.
Signature:
- Kotlin
- Java
What this does: Declares theUsage:sendFormMessage()method signature. It accepts aFormMessageobject and aCallbackListenerthat returns the sentFormMessageon success.
- Kotlin
- Java
What this does: Creates aFormMessagewith three text input fields (Name, Last Name, Address) and a submit button that navigates to a URL. The form is sent to useruid-1001and allows the sender to interact with it. The message appears as an interactive form bubble in the Message List.
Card Message
To send a Card message to a single user or a group, utilize thesendCardMessage() function. This function requires a CardMessage object as its argument, which contains the necessary information to create a card bubble for the message.
Signature:
- Kotlin
- Java
What this does: Declares theUsage:sendCardMessage()method signature. It accepts aCardMessageobject and aCallbackListenerthat returns the sentCardMessageon success.
- Kotlin
- Java
What this does: Creates a CardMessage with decorative text, an image URL, and a “Learn more” button that navigates to a URL. The card is sent to the specified receiver and appears as an interactive card bubble in the Message List.
Scheduler Message
To send a Scheduler message to a single user or a group, use thesendSchedulerMessage() function. This function requires a SchedulerMessage object as its argument, which contains the necessary information to create a SchedulerMessage bubble for the messages.
Signature:
- Kotlin
- Java
What this does: Declares theUsage:sendSchedulerMessage()method signature. It accepts aSchedulerMessageobject and aCallbackListenerthat returns the sentSchedulerMessageon success.
- Kotlin
- Java
What this does: Creates a SchedulerMessage configured with a 60-minute meeting duration, 15-minute buffer, weekly availability slots, and a submit button that posts to an API endpoint. The scheduler bubble lets the receiver pick a time slot and appears in the Message List.
Custom InteractiveMessage
To send a Custom Interactive message to a single user or a group, use thesendCustomInteractiveMessage() function. This function requires a CustomInteractiveMessage object as its argument.
Signature:
- Kotlin
- Java
What this does: Declares theUsage:sendCustomInteractiveMessage()method signature. It accepts aCustomInteractiveMessageobject and aCallbackListenerthat returns the sentCustomInteractiveMessageon success.
- Kotlin
- Java
What this does: Creates a CustomInteractiveMessage with a JSON payload and sends it through the UI Kit. The message is delivered to the specified receiver and can be rendered with a custom template.
Note: To display custom messages in the MessageList, you must create and register a new MessageTemplate that defines how to render your custom message types
Common pitfalls and fixes
| Pitfall | Fix |
|---|---|
Calling UI Kit methods before CometChatUIKit.init() completes | Call init() first and wait for the onSuccess callback before calling login(), sendTextMessage(), or any other UI Kit method. |
| Using Auth Key in production | Auth Key is for development and POC only. For production apps, use CometChatUIKit.loginWithAuthToken() with a server-generated Auth Token so the Auth Key never appears in client code. |
Not handling onError callbacks | Every UI Kit method accepts a CallbackListener with onError. If you leave onError empty, failures are silently ignored. Log the CometChatException or display an error message to the user. |
Calling CometChat.sendTextMessage() instead of CometChatUIKit.sendTextMessage() | If you use the SDK method directly (CometChat.sendTextMessage()), the Message List and Conversations components do not update automatically. Use the CometChatUIKit wrapper methods to keep UI components in sync. |
| Not logging out before switching users | If you log in a new user without calling CometChatUIKit.logout() first, session data from the previous user may cause conflicts. Call logout() and wait for onSuccess before logging in a different user. |
Sending a custom message without a registered MessageTemplate | Custom messages and custom interactive messages do not appear in the Message List unless you create and register a MessageTemplate that defines how to render them. |
Missing receiver or sender on interactive messages | For FormMessage, CardMessage, SchedulerMessage, and CustomInteractiveMessage, set both sender (via CometChatUIKit.getLoggedInUser()) and receiver (the target User or Group object) before sending. |
FAQ
What is the difference betweenCometChatUIKit.sendTextMessage() and CometChat.sendTextMessage()?
CometChatUIKit.sendTextMessage() is a wrapper around the SDK method that automatically updates the Message List and Conversations components via internal events. CometChat.sendTextMessage() only sends the message over the network and does not trigger UI Kit component updates. The same pattern applies to sendMediaMessage(), sendCustomMessage(), and all interactive message methods.
When should I use Auth Key vs Auth Token for login?
Use CometChatUIKit.login() with Auth Key during development and proof-of-concept work. For production apps, use CometChatUIKit.loginWithAuthToken() with a server-generated Auth Token. This keeps the Auth Key on your server and out of client code.
How do I customize date and time formatting across all UI Kit components?
Implement the DateTimeFormatterCallback interface and pass it to UIKitSettings.UIKitSettingsBuilder via setDateTimeFormatterCallback() before calling CometChatUIKit.init(). Each method in the interface (time, today, yesterday, lastWeek, otherDays, minutes, hours) controls a specific timestamp display case.
Do I need to call init() every time the app launches?
Yes. Call CometChatUIKit.init() once during your application’s startup lifecycle (for example, in your Application class or main Activity’s onCreate). The UI Kit and SDK require initialization before any other method call.
How do I send a message to a group instead of a user?
Use CometChatConstants.RECEIVER_TYPE_GROUP instead of CometChatConstants.RECEIVER_TYPE_USER when creating the message object, and pass the group ID as the receiver ID.
Next steps
- Events reference — Learn how to listen for real-time UI Kit events (user, group, conversation, message, call, and UI events).
- Message List component — Display and customize the message list where sent messages appear.
- Conversations component — Display and customize the conversations list that updates when messages are sent.