Skip to main content
Using CometChat, you can work with four types of messages. The first three you can send directly:
  1. A Text Message, the most common and standard message type.
  2. A Media Message, for sending photos, videos and files.
  3. A Custom Message, for sending completely custom data using JSON structures.
The fourth is receive-only:
  1. A Card Message, a structured, interactive card message created server-side via the REST API and delivered to clients.
You can also send metadata along with a text or media message. Think, for example, if you’d want to share the user’s location with every message, you can use the metadata field.

Text Message

In other words, as a sender, how do I send a text message? To send a text message to a single user or group, you need to use the sendMessage() method and pass a TextMessage object to it.

Add Metadata

To send custom data along with a text message, you can use the setMetadata method and pass a JSONObject to it.

Add Tags

To add a tag to a message you can use the setTags() method of the TextMessage Class. The setTags() method accepts a list of tags.

Set Quoted Message

To set a quoted message for a message, use the setQuotedMessageId() and setQuotedMessage() method of the TextMessage class. This method accepts the ID of the message to be quoted.
Once the text message object is ready, you need to use the sendMessage() method to send the text message to the recipient.
The TextMessage class constructor takes the following parameters: When a text message is sent successfully, the response will include a TextMessage object which includes all information related to the sent message.

Media Message

In other words, as a sender, how do I send a media message like photos, videos & files? To send a media message to any user or group, you need to use the sendMediaMessage() method and pass a MediaMessage object to it.

Add Metadata

To send custom data along with a media message, you can use the setMetadata method and pass a JSONObject to it.

Add Caption(Text along with Media Message)

To send a caption with a media message, you can use setCaption method and pass text to it.

Add Tags

To add a tag to a message you can use the setTags() method of the MediaMessage Class. The setTags() method accepts a list of tags.
There are 2 ways you can send Media Messages using the CometChat SDK:
  1. By providing the File : You can directly share the file object while creating an object of the MediaMessage class. When the media message is sent using the sendMediaMessage() method, this file is then uploaded to CometChat servers and the URL of the file is sent in the success response of the sendMediaMessage() function.
The MediaMessage class constructor takes the following parameters:
  1. By providing the URL of the File: The second way to send media messages using the CometChat SDK is to provide the SDK with the URL of any file that is hosted on your servers or any cloud storage. To achieve this you will have to make use of the Attachment class that is available in the MediaMessage class. For more information, you can refer to the below code snippet:
When a media message is sent successfully, the response will include a MediaMessage object which includes all information related to the sent message. If you wish to send a caption or some text along with the Media Message, you can use the caption field provided by the MediaMessage class. To set the caption you can use the setCaption() method and at the receiver end, you can obtain the caption using the getCaption() method. As with text messages, the metadata field can be used with media messages as well. Any additional information can be passed along with the media message as a JSONObject.

Multiple Attachments in a Media Message

To send several attachments in one media message, use the upload-first flow: upload the files with CometChat.uploadFiles(), collect the returned Attachment objects, then send a single MediaMessage carrying all of them. Decoupling upload from send gives you per-file progress, per-file error handling, and the ability to cancel or retry individual uploads — which is what a WhatsApp-style attachment tray needs. Send is then instant, because the bytes are already on storage.

Step 1: Upload the Files

Pass the files, the receiver, and a MediaUploadListener to CometChat.uploadFiles(). It returns synchronously with an UploadHandle — the upload-group muid and one stable fileId per file, in input order (fileIds.get(i) corresponds to files.get(i)). Key each UI tile by its fileId so the per-file callbacks map to the right tile.
A second overload accepts a List<Uri> for content URIs coming from the Android document/media pickers; a Context is required so the SDK can resolve each URI’s name, size, and mime type:
To add more files to the same group later (a second pick, a paste, a drag-and-drop), reuse the muid through UploadOptions; raise concurrency only on known-fast networks:
Implement MediaUploadListener to track each file. onFileError and onFileFailure are two distinct outcomes — exactly one fires per non-successful file:
To cancel an in-flight upload (for example when the user removes a staged file) or retry a failed one:

Step 2: Send the Message

Once the uploads you need have succeeded, build a MediaMessage with the no-file constructor, attach the collected Attachment objects with setAttachments(), carry over the muid for reconciliation, and send it with the existing sendMediaMessage(): A message’s attachments must all match the message’s own type — the three images below go out as one MESSAGE_TYPE_IMAGE message. If your pick mixes types, group the uploaded attachments by type and send one message per group (images → videos → audios → files), reusing the same muid across the sends. This is exactly what the UI Kit composer does for you.
Build the message with the constructor that takes no filesMediaMessage(receiverId, messageType, receiverType) — when sending pre-uploaded attachments. If you also pass File objects, the SDK re-uploads them on send, which defeats the upload-first flow.

Upload Limits

The per-message file count and per-file size are governed by your app’s settings (configured in the CometChat dashboard) and can be read at runtime:
Use these to cap your picker’s selection and pre-validate file sizes before uploading. The SDK also validates each file before requesting an upload URL, and the server validates size and content-type when issuing upload URLs — a file that fails validation is reported through onFileError or onFileFailure while the rest of the batch continues.

Error Codes

Errors arrive as a CometChatException on either onFileError (rejected — retrying cannot succeed) or onFileFailure (transfer failed — retryable). Read getCode() to branch:
uploadFiles() does not enforce a file-count cap — you may upload any number of files. The per-message limit (file.count.max) is enforced at send time: sendMediaMessage() calls back onError with ERR_FILE_COUNT_EXCEEDED if a message carries more attachments than allowed.

Legacy: Direct Send with Files

The older direct-send form (version 3.0.9 & above) still works: pass the files on the MediaMessage itself and the SDK uploads them as part of the send. It provides no per-file progress, cancellation, or per-file error handling, so prefer the upload-first flow above for new integrations. There are two ways:
  1. By providing an array of files: You can now share a List of files while creating an object of the MediaMessage class. When the media message is sent using the sendMediaMessage() method, the files are uploaded to the CometChat servers & the URL of the files are sent in the success response of the sendMediaMessage() method.
The MediaMessage class constructor takes the following parameters:
  1. By providing the URL of the multiple files: The second way to send multiple attachments using the CometChat SDK is to provide the SDK with the URL of multiple files that is hosted on your servers or any cloud storage. To achieve this you will have to make use of the Attachment class. For more information, you can refer to the below code snippet:
When a media message is sent successfully, the response will include a MediaMessage object which includes all information related to the sent message. You can use the setMetadata(), setCaption() & setTags() methods to add metadata, caption and tags respectively in exactly the same way as it is done while sending a single file or attachment in a Media Message.

Custom Message

In other words, as a sender, how do I send a custom message like location co-ordinates? CometChat allows you to send custom messages which are neither text nor media messages. In order to send a custom message, you need to use the sendCustomMessage() method. The sendCustomMessage() methods takes an object of the CustomMessage which can be obtained using the below constructor.
The above constructor, helps you create a custom message with the message type set to whatever is passed to the constructor and the category set to custom. The parameters involved are:
  1. receiverId - Unique id of the user or group to which the message is to be sent.
  2. receiverType - Type of the receiver i.e user or group
  3. customType - custom message type that you need to set
  4. customData - The data to be passed as the message in the form of a JSONObject.
You can also use the subType field of the CustomMessage class to set a specific type for the custom message. This can be achieved using the setSubtype() method.

Add Tags

To add a tag to a message you can use the setTags() method of the CustomMessage Class. The setTags() method accepts a list of tags.
Once the object of CustomMessage class is ready you can send the custom message using the sendCustomMessage() method.
The above sample explains how custom messages can be used to share the location with a user. Similarly, you can send custom messages to groups. On success, you will receive an object of CustomMessage class.

Update Conversation

How can I decide whether the custom message should update the last message of a conversation? By default, a custom message will update the last message of a conversation. If you wish to not update the last message of the conversation when a custom message is sent, please use shouldUpdateConversation(boolean value) method of the Custom Message.

Custom Notification Body

How can i customise the notification body of custom message? To add a custom notification body for Push, Email & SMS notification of a custom message you can use the setConversationText(text: string) method of Custom Message class.

Card Message

In other words, as a sender, how do I send a card message? A CardMessage is a structured, interactive message rendered as a card bubble. It belongs to the card category and carries a block of card schema JSON that the CometChat Cards library draws.
Card Messages cannot be sent through the SDK. The CardMessage class is receive-only — it has no public constructor and the SDK exposes no sendCardMessage() method. Card Messages are created server-side via the Platform REST API or the Dashboard Bubble Builder, and delivered to clients like any other message.To create and send a Card Message, use the REST API. See the Send Message REST API reference for the message creation flow.
On the receiving end, the CardMessage object gives you access to the card payload and its related fields.
The CardMessage class provides the following methods: CardMessage extends BaseMessage, so it also exposes the standard message fields (getId(), getSender(), getReceiverUid(), getSentAt(), getCategory() = card, etc.). To handle incoming Card Messages on the client, implement onCardMessageReceived on your MessageListener.

Render a Card Message

A CardMessage carries raw card-schema JSON in getCard(). To draw it natively, use the CometChat Cards renderer library (com.cometchat:cards-android) — the same renderer the UI Kit’s card bubble wraps. It is a pure renderer: you hand it the card JSON and an action callback, and you own all action behavior.

Add the Cards dependency

Requires minSdk 24, Kotlin, and the internet permission in your AndroidManifest.xml.

Render the card

Pass the card JSON (cardMessage.getCard().toString()) to the renderer and handle actions through the callback.
The Cards library is a pure renderer — it never executes actions, it only emits them through the callback. You own all behavior (opening URLs, navigating to chats, API calls, etc.). The 9 action types (CometChatCardOpenUrlAction, CometChatCardChatWithUserAction, …) live in com.cometchat.cards.models, and event is a com.cometchat.cards.actions.CometChatCardActionEvent exposing action, elementId, and cardJson.When rendering directly, fallback is your responsibility: if the card JSON is empty or invalid, fall back to cardMessage.getFallbackText() (then getText()).