Skip to main content

Documentation Index

Fetch the complete documentation index at: https://cometchat-22654f5b-docs-android-v6-beta2.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

// Text message
val textMessage = TextMessage("UID", "Hello!", CometChatConstants.RECEIVER_TYPE_USER)
CometChat.sendMessage(textMessage, object : CallbackListener<TextMessage>() {
    override fun onSuccess(msg: TextMessage) { }
    override fun onError(e: CometChatException) { }
})

// Media message (file upload)
val mediaMessage = MediaMessage("UID", File("/path/to/file.jpg"),
    CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_USER)
CometChat.sendMediaMessage(mediaMessage, object : CallbackListener<MediaMessage>() {
    override fun onSuccess(msg: MediaMessage) { }
    override fun onError(e: CometChatException) { }
})

// Custom message
val customData = JSONObject().apply { put("latitude", "19.07"); put("longitude", "72.87") }
val customMessage = CustomMessage("UID", CometChatConstants.RECEIVER_TYPE_USER, "LOCATION", customData)
CometChat.sendCustomMessage(customMessage, object : CallbackListener<CustomMessage>() {
    override fun onSuccess(msg: CustomMessage) { }
    override fun onError(e: CometChatException) { }
})
TypeMethodReceiver Types
TextsendMessage()RECEIVER_TYPE_USER, RECEIVER_TYPE_GROUP
MediasendMediaMessage()RECEIVER_TYPE_USER, RECEIVER_TYPE_GROUP
CustomsendCustomMessage()RECEIVER_TYPE_USER, RECEIVER_TYPE_GROUP
CometChat supports three types of messages:
TypeMethodUse Case
TextsendMessage()Plain text messages
MediasendMediaMessage()Images, videos, audio, files
CustomsendCustomMessage()Location, polls, or any JSON data
You can also send Interactive Messages for forms, cards, and custom UI elements.

Text Message

Send a text message using sendMessage() with a TextMessage object.
private String receiverID = "UID";
private String messageText = "Hello CometChat!";
private String receiverType = CometChatConstants.RECEIVER_TYPE_USER;

TextMessage textMessage = new TextMessage(receiverID, messageText, receiverType);

CometChat.sendMessage(textMessage, new CometChat.CallbackListener<TextMessage>() {
  @Override
  public void onSuccess(TextMessage textMessage) {
      Log.d(TAG, "Message sent successfully: " + textMessage.toString());
  }

  @Override
  public void onError(CometChatException e) {
      Log.d(TAG, "Message sending failed with exception: " + e.getMessage());
  }
});
The TextMessage class constructor takes the following parameters:
ParameterDescriptionRequired
receiverIDUID of the user or GUID of the group receiving the messageYes
messageTextThe text message contentYes
receiverTypeCometChatConstants.RECEIVER_TYPE_USER or RECEIVER_TYPE_GROUPYes
On success, sendMessage() returns a TextMessage object containing all information about the sent message.

Add Metadata

Attach custom JSON data to the message:
JSONObject metadata = new JSONObject();
metadata.put("latitude", "50.6192171633316");
metadata.put("longitude", "-72.68182268750002");
textMessage.setMetadata(metadata);

Add Tags

Tag messages for easy filtering later:
List<String> tags = new ArrayList<>();
tags.add("pinned");
textMessage.setTags(tags);

Quote a Message

Reply to a specific message by setting its ID:
textMessage.setQuotedMessageId(10);

Media Message

Send images, videos, audio, or files using sendMediaMessage(). There are two ways to send media messages:
  1. Upload a file — Pass a File object and CometChat uploads it automatically
  2. Send a URL — Provide a URL to media hosted on your server or cloud storage

Add Metadata

JSONObject metadata = new JSONObject();
metadata.put("latitude", "50.6192171633316");
metadata.put("longitude", "-72.68182268750002");
mediaMessage.setMetadata(metadata);

Add Caption

Add text along with the media:
mediaMessage.setCaption("Message Caption");

Add Tags

List<String> tags = new ArrayList<>();
tags.add("pinned");
mediaMessage.setTags(tags);

Upload a File

Pass a File object directly. CometChat uploads it to its servers and returns the URL in the success response.
private String receiverID = "UID";
private String messageType = CometChatConstants.MESSAGE_TYPE_IMAGE;
private String receiverType = CometChatConstants.RECEIVER_TYPE_USER;
private String filePath = "/storage/emulated/0/Download/CometChat.jpg";

MediaMessage mediaMessage = new MediaMessage(receiverID, new File(filePath), messageType, receiverType);

CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() {
  @Override
  public void onSuccess(MediaMessage mediaMessage) {
      Log.d(TAG, "Media message sent successfully: " + mediaMessage.toString());
  }

  @Override
  public void onError(CometChatException e) {
      Log.d(TAG, "Media message sending failed with exception: " + e.getMessage());
  }
});
The MediaMessage class constructor takes the following parameters:
ParameterDescriptionRequired
receiverIdThe UID or GUID of the recipientYes
fileThe File object to uploadYes
messageTypeMESSAGE_TYPE_IMAGE, MESSAGE_TYPE_VIDEO, MESSAGE_TYPE_AUDIO, or MESSAGE_TYPE_FILEYes
receiverTypeRECEIVER_TYPE_USER or RECEIVER_TYPE_GROUPYes

Send a URL

Send media hosted on your server or cloud storage using the Attachment class:
String receiverId = "recipient_UID";

MediaMessage mediaMessage = new MediaMessage(receiverId, CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_USER);

Attachment attachment = new Attachment();
attachment.setFileName("test");
attachment.setFileExtension("png");
attachment.setFileUrl("https://pngimg.com/uploads/mario/mario_PNG125.png");

mediaMessage.setAttachment(attachment);

CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() {
  @Override
  public void onSuccess(MediaMessage mediaMessage1) {
      Log.i(TAG, "onSuccess");
  }

  @Override
  public void onError(CometChatException e) {
      Log.e(TAG, e.getMessage());
  }
});
On success, sendMediaMessage() returns a MediaMessage object.

Multiple Attachments

Starting version 3.0.9, the SDK supports sending multiple attachments in a single media message.

Upload Multiple Files

String receiverId = "cometchat-uid-1";

List<File> files = new ArrayList<File>();
files.add(new File("/storage/emulated/0/Download/1.jpg"));
files.add(new File("/storage/emulated/0/Download/2.jpg"));
files.add(new File("/storage/emulated/0/Download/3.jpg"));

MediaMessage mediaMessage = new MediaMessage(receiverId, files, fileType, CometChatConstants.RECEIVER_TYPE_USER);

CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() {
  @Override
  public void onSuccess(MediaMessage mediaMessage) {
      Log.d(TAG, "Message Sent Successfully")
  }

  @Override
  public void onError(CometChatException e) {
      Log.d(TAG, "Message Sending Failed with exception : " + e.getMessage());
  }
});
The MediaMessage class constructor takes the following parameters:
ParameterDescriptionRequired
receiverIdThe UID or GUID of the recipientYes
filesA List<File> of files to uploadYes
messageTypeMESSAGE_TYPE_IMAGE, MESSAGE_TYPE_VIDEO, MESSAGE_TYPE_AUDIO, or MESSAGE_TYPE_FILEYes
receiverTypeRECEIVER_TYPE_USER or RECEIVER_TYPE_GROUPYes

Send Multiple URLs

String receiverId = "cometchat-uid-1";

MediaMessage mediaMessage = new MediaMessage(receiverId, CometChatConstants.MESSAGE_TYPE_IMAGE, CometChatConstants.RECEIVER_TYPE_USER);

List<Attachment> attachments = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    Attachment attachment = new Attachment();
    attachment.setFileName("test");
    attachment.setFileExtension("png");
    attachment.setFileUrl("https://pngimg.com/uploads/mario/mario_PNG125.png");
    attachment.setFileMimeType("image/png");
    attachments.add(attachment);
}

mediaMessage.setAttachments(attachments);

CometChat.sendMediaMessage(mediaMessage, new CometChat.CallbackListener<MediaMessage>() {
    @Override
    public void onSuccess(MediaMessage mediaMessage) {
        Log.d(TAG, "Message Sent Successfully")
    }

    @Override
    public void onError(CometChatException e) {
        Log.d(TAG, "Message Sending Failed with exception : " + e.getMessage());
    }
});
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 in the same way as a single attachment.

Custom Message

Send custom data that doesn’t fit text or media using sendCustomMessage().
CustomMessage customMessage = new CustomMessage(receiverId, receiverType, customType, customData)
ParameterDescriptionRequired
receiverIdUID of the user or GUID of the groupYes
receiverTypeRECEIVER_TYPE_USER or RECEIVER_TYPE_GROUPYes
customTypeDeveloper-defined type string (e.g., "LOCATION", "POLL")Yes
customDataThe payload as a JSONObjectYes
You can also set a subtype using setSubtype() to further classify the custom message.

Add Tags

List<String> tags = new ArrayList<>();
tags.add("pinned");
customMessage.setTags(tags);
Once the message object is ready, call sendCustomMessage().
private String UID = "UID";
private String customType = "LOCATION";

JSONObject customData = new JSONObject();
customData.put("latitude", "19.0760");
customData.put("longitude", "72.8777");

CustomMessage customMessage = new CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData);

CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() {
  @Override
  public void onSuccess(CustomMessage customMessage) {
      Log.d(TAG, customMessage.toString());
  }

  @Override
  public void onError(CometChatException e) {
      Log.d(TAG, e.getMessage());
  }
});
On success, sendCustomMessage() returns a CustomMessage object.

Control Conversation Update

By default, a custom message updates the conversation’s last message. To prevent this:
private String UID = "UID";
private String customType = "LOCATION";

JSONObject customData = new JSONObject();
customData.put("latitude", "19.0760");
customData.put("longitude", "72.8777");

CustomMessage customMessage = new CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData);
customMessage.shouldUpdateConversation(false);

CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() {
  @Override
  public void onSuccess(CustomMessage customMessage) {
      Log.d(TAG, customMessage.toString());
  }

  @Override
  public void onError(CometChatException e) {
      Log.d(TAG, e.getMessage());
  }
});

Custom Notification Text

Set custom text for push, email, and SMS notifications:
private String UID = "UID";
private String customType = "LOCATION";

JSONObject customData = new JSONObject();
customData.put("latitude", "19.0760");
customData.put("longitude", "72.8777");

CustomMessage customMessage = new CustomMessage(UID, CometChatConstants.RECEIVER_TYPE_USER, customType, customData);
customMessage.setConversationText("Custom Notification Body");

CometChat.sendCustomMessage(customMessage, new CometChat.CallbackListener<CustomMessage>() {
  @Override
  public void onSuccess(CustomMessage customMessage) {
      Log.d(TAG, customMessage.toString());
  }

  @Override
  public void onError(CometChatException e) {
      Log.d(TAG, e.getMessage());
  }
});
It is also possible to send interactive messages from CometChat, to know more click here

Next Steps

Receive Messages

Listen for incoming messages in real time

Edit Message

Modify sent messages after delivery

Delete Message

Remove messages from conversations

Interactive Messages

Send messages with embedded forms and buttons

Message Payload Structure

The BaseMessage object returned by SDK methods contains the following fields. TextMessage, MediaMessage, and CustomMessage extend this base structure with additional type-specific fields.
ParameterTypeDescription
idlongUnique message identifier
muidStringDeveloper-defined message ID for deduplication
senderUserUser who sent the message
receiverAppEntityMessage receiver (User or Group)
receiverUidStringReceiver’s unique identifier
typeStringMessage type. Values: "text", "image", "video", "audio", "file", "custom"
receiverTypeStringType of receiver. Values: "user", "group"
categoryStringMessage category. Values: "message", "action", "call", "custom"
sentAtlongUnix timestamp when message was sent
deliveredAtlongUnix timestamp when message was delivered
readAtlongUnix timestamp when message was read
metadataJSONObjectCustom message metadata set by developer
readByMeAtlongUnix timestamp when logged-in user read the message
deliveredToMeAtlongUnix timestamp when message was delivered to logged-in user
deletedAtlongUnix timestamp when message was deleted (0 if not deleted)
editedAtlongUnix timestamp when message was edited (0 if not edited)
deletedByStringUID of user who deleted the message (null if not deleted)
editedByStringUID of user who edited the message (null if not edited)
updatedAtlongUnix timestamp of last message update
conversationIdStringAssociated conversation identifier
parentMessageIdlongParent message ID for threaded messages (0 if not a reply)
replyCountintNumber of replies in thread
unreadRepliesCountintNumber of unread replies in thread
mentionedUsersArray<User>List of users mentioned in the message
hasMentionedMebooleanWhether the logged-in user is mentioned
reactionsArray<ReactionCount>List of reaction counts on the message
rawMessageJSONObjectRaw JSON message data
quotedMessageIdlongID of the quoted message (0 if not quoting)
quotedMessageBaseMessageThe quoted message object (null if not quoting)
Sample BaseMessage Object:
{
  "id": 12345,
  "muid": "msg_abc123",
  "sender": {
    "uid": "user_123",
    "name": "John Doe",
    "avatar": "https://example.com/avatar.png",
    "status": "online",
    "role": "default",
    "lastActiveAt": 1699900000
  },
  "receiver": {
    "uid": "user_456",
    "name": "Jane Smith",
    "avatar": "https://example.com/avatar2.png"
  },
  "receiverUid": "user_456",
  "type": "text",
  "receiverType": "user",
  "category": "message",
  "sentAt": 1699900000,
  "deliveredAt": 1699900001,
  "readAt": 1699900002,
  "metadata": {
    "priority": "high",
    "customField": "value"
  },
  "readByMeAt": 1699900002,
  "deliveredToMeAt": 1699900001,
  "deletedAt": 0,
  "editedAt": 0,
  "deletedBy": null,
  "editedBy": null,
  "updatedAt": 1699900000,
  "conversationId": "user_123_user_456",
  "parentMessageId": 0,
  "replyCount": 5,
  "unreadRepliesCount": 2,
  "mentionedUsers": [],
  "hasMentionedMe": false,
  "reactions": [
    {
      "reaction": "👍",
      "count": 3,
      "reactedByMe": true
    }
  ],
  "rawMessage": {},
  "quotedMessageId": 0,
  "quotedMessage": null
}
The nested User object (used in sender, receiver, mentionedUsers) contains:
ParameterTypeDescription
uidStringUnique identifier of the user
nameStringDisplay name of the user
avatarStringURL to user’s profile picture
linkStringURL to user’s profile page
roleStringUser role for access control
metadataJSONObjectCustom data set by developer
statusStringUser online status. Values: "online", "offline"
statusMessageStringCustom status message
lastActiveAtlongUnix timestamp of last activity
hasBlockedMebooleanWhether this user has blocked the logged-in user
blockedByMebooleanWhether the logged-in user has blocked this user
tagsArray<String>List of tags for user identification
deactivatedAtlongUnix timestamp when user was deactivated (0 if active)
The nested Group object (used in receiver for group messages) contains:
ParameterTypeDescription
guidStringUnique identifier of the group
nameStringDisplay name of the group
typeStringGroup type. Values: "public", "private", "password"
iconStringURL to group icon
descriptionStringGroup description
ownerStringUID of group owner
metadataJSONObjectCustom data set by developer
membersCountintNumber of group members
tagsArray<String>List of tags for group identification
hasJoinedbooleanWhether logged-in user has joined
scopeStringUser’s scope in group. Values: "admin", "moderator", "participant"
The nested ReactionCount object (used in reactions array) contains:
ParameterTypeDescription
reactionStringThe reaction emoji
countintTotal count of this reaction
reactedByMebooleanWhether the logged-in user has reacted with this emoji