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.

FieldValue
Key ClassesTextMessage, MediaMessage, CustomMessage, MessagesRequest, MessagesRequestBuilder
Key MethodsCometChat.sendMessage(), CometChat.sendMediaMessage(), CometChat.sendCustomMessage(), MessagesRequest.fetchPrevious()
Key PropertiesparentMessageId, hideReplies
Listener EventsonTextMessageReceived, onMediaMessageReceived, onCustomMessageReceived
PrerequisitesSDK initialized, user logged in
// Send a message in a thread (attach to parent message)
TextMessage textMessage = TextMessage(
  text: "Reply in thread",
  receiverUid: "UID",
  receiverType: CometChatReceiverType.user,
  type: CometChatMessageType.text,
);
textMessage.parentMessageId = 103; // Parent message ID
CometChat.sendMessage(textMessage, onSuccess: (msg) {}, onError: (e) {});

// Fetch messages from a thread
MessagesRequest messageRequest = (MessagesRequestBuilder()
  ..uid = "UID"
  ..parentMessageId = 103
  ..limit = 50).build();
messageRequest.fetchPrevious(onSuccess: (List<BaseMessage> list) {}, onError: (e) {});

// Exclude threaded messages from main conversation
MessagesRequest request = (MessagesRequestBuilder()
  ..uid = "UID"
  ..hideReplies = true
  ..limit = 50).build();
Threaded messages (or threads) are messages started from a particular parent message. Each thread is attached to a parent message.
Available via: SDK | REST API | UI Kits

Send Message in a Thread

As mentioned in the Send a Message section, you can send a message to a User or a Group by mentioning the receiver (uid/guid) and receiverType(user/group). A message can be categorized as:
  1. Text Message
  2. Media Message
  3. Custom Message
  4. Interactive Message
Set the parentMessageId on the message object to indicate which thread the message belongs to. The id specified in the parentMessageId parameter maps the message sent to the particular thread. Any message type — TextMessage, MediaMessage, CustomMessage, or Interactive Message — can be sent in a thread.
String receiverID = "UID";
String messagesText = "Hello";
String receiverType = CometChatConversationType.user;
String type = CometChatMessageType.text;

TextMessage textMessage = TextMessage(
                        text: messagesText,
                        receiverUid: receiverID,
                        receiverType: receiverType,
                        type: type);
textMessage.parentMessageId = 103;

CometChat.sendMessage(textMessage, onSuccess: (TextMessage message) {
  debugPrint("Message sent successfully:  $message");
}, onError: (CometChatException e) {
  debugPrint("Message sending failed with exception:  ${e.message}");
});
TextMessage Parameters:
ParameterTypeDescriptionRequired
textStringThe text content of the messageYes
receiverUidStringThe UID of the user or GUID of the group to send the message toYes
receiverTypeStringThe type of the receiver — CometChatConversationType.user or CometChatConversationType.groupYes
typeStringThe type of the message — CometChatMessageType.textYes
parentMessageIdintThe ID of the parent message to send this message as a thread replyYes (for threads)
On Success — A TextMessage object containing all details of the sent threaded message:TextMessage Object:
ParameterTypeDescriptionSample Value
idnumberUnique message ID601
metadataobjectCustom metadata attached to the message{}
receiverobjectReceiver user objectSee below ↓
editedBystringUID of the user who edited the messagenull
conversationIdstringUnique conversation identifier"cometchat-uid-1_user_cometchat-uid-2"
sentAtnumberEpoch timestamp when the message was sent1745554729
receiverUidstringUID of the receiver"cometchat-uid-2"
typestringType of the message"text"
readAtnumberEpoch timestamp when the message was read0
deletedBystringUID of the user who deleted the messagenull
deliveredAtnumberEpoch timestamp when the message was delivered0
deletedAtnumberEpoch timestamp when the message was deleted0
replyCountnumberNumber of replies to this message0
senderobjectSender user objectSee below ↓
receiverTypestringType of the receiver"user"
editedAtnumberEpoch timestamp when the message was edited0
parentMessageIdnumberID of the parent message (for threads)103
readByMeAtnumberEpoch timestamp when read by the current user0
categorystringMessage category"message"
deliveredToMeAtnumberEpoch timestamp when delivered to the current user0
updatedAtnumberEpoch timestamp when the message was last updated1745554729
textstringThe text content of the message"Hello"
tagsarrayList of tags associated with the message[]
unreadRepliesCountnumberCount of unread replies0
mentionedUsersarrayList of mentioned users[]
hasMentionedMebooleanWhether the current user is mentionedfalse
reactionsarrayList of reactions on the message[]
moderationStatusstringModeration status of the messagenull
quotedMessageIdnumberID of the quoted messagenull

sender Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the sender"cometchat-uid-1"
namestringDisplay name of the sender"Andrew Joseph"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
metadataobjectCustom metadata{}
statusstringOnline status"online"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745554700

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the receiver"cometchat-uid-2"
namestringDisplay name of the receiver"George Alan"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
metadataobjectCustom metadata{}
statusstringOnline status"offline"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745550000
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_CHAT_API_FAILURE"
messagestringHuman-readable error message"Failed to send the message."
detailsstringAdditional technical details"An unexpected error occurred while processing the request."
The above snippet shows how a message with the text “Hello” can be sent in the thread with parentMessageId 103. Similarly, using the parentMessageId parameter, Media and Custom Messages can be sent in threads too.

Receiving Real-Time Messages

The procedure to receive real-time messages is exactly the same as mentioned in the Receive Messages section. Use the MessageListener class to listen for incoming thread messages. Check if the received message belongs to the active thread using the parentMessageId field.
Always remove message listeners when they’re no longer needed (e.g., in the dispose() method). Failing to remove listeners can cause memory leaks and duplicate event handling.
CometChat.removeMessageListener("listenerId");
class Class_Name  with MessageListener {
	int activeParentMessageId= 103;
//CometChat.addMessageListener("listenerId", this);
	@override
void onTextMessageReceived(TextMessage textMessage) {
   if(textMessage.parentMessageId == activeParentMessageId){
      debugPrint("Text message received successfully: $textMessage");
  }

}

@override
void onMediaMessageReceived(MediaMessage mediaMessage) {
  if(mediaMessage.parentMessageId== activeParentMessageId){
  	debugPrint("Media message received successfully: $mediaMessage");
  }
}

@override
void onCustomMessageReceived(CustomMessage customMessage) {
  if(customMessage.parentMessageId== activeParentMessageId){
    debugPrint("Custom message received successfully: $customMessage");
  }
}
}

Fetch all the messages for any particular thread.

Use MessagesRequestBuilder with parentMessageId to fetch messages belonging to a specific thread. Call fetchPrevious() to get messages (max 100 per request), returned as BaseMessage objects. Call fetchPrevious() again on the same object to get the next set. MessagesRequestBuilder Parameters:
ParameterTypeDescriptionRequired
uidStringThe UID of the user whose conversation thread messages are to be fetchedYes (for user threads)
guidStringThe GUID of the group whose conversation thread messages are to be fetchedYes (for group threads)
parentMessageIdintThe ID of the parent message whose thread messages are to be fetchedYes
limitintNumber of messages to fetch in a single request (max 100)No
String UID = "cometchat-uid-1";

MessagesRequest messageRequest = (MessagesRequestBuilder()
          ..uid = UID
          ..parentMessageId = 103
          ..limit = 50).build();

messageRequest.fetchPrevious(onSuccess: (List<BaseMessage> list) {
         debugPrint("Message fetching Successful");
      }, onError: (CometChatException e) {
        debugPrint("Message fetching failed with exception: ${e.message}");
      });
On Success — A List<BaseMessage> containing the fetched thread messages (each item is a BaseMessage object):BaseMessage Object:
ParameterTypeDescriptionSample Value
idnumberUnique message ID602
metadataobjectCustom metadata attached to the message{}
receiverobjectReceiver user objectSee below ↓
editedBystringUID of the user who edited the messagenull
conversationIdstringUnique conversation identifier"cometchat-uid-1_user_cometchat-uid-2"
sentAtnumberEpoch timestamp when the message was sent1745554729
receiverUidstringUID of the receiver"cometchat-uid-1"
typestringType of the message"text"
readAtnumberEpoch timestamp when the message was read0
deletedBystringUID of the user who deleted the messagenull
deliveredAtnumberEpoch timestamp when the message was delivered0
deletedAtnumberEpoch timestamp when the message was deleted0
replyCountnumberNumber of replies to this message0
senderobjectSender user objectSee below ↓
receiverTypestringType of the receiver"user"
editedAtnumberEpoch timestamp when the message was edited0
parentMessageIdnumberID of the parent message (for threads)103
readByMeAtnumberEpoch timestamp when read by the current user0
categorystringMessage category"message"
deliveredToMeAtnumberEpoch timestamp when delivered to the current user0
updatedAtnumberEpoch timestamp when the message was last updated1745554729
unreadRepliesCountnumberCount of unread replies0
quotedMessageIdnumberID of the quoted messagenull
quotedMessageobjectThe quoted message objectnull

sender Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the sender"cometchat-uid-2"
namestringDisplay name of the sender"George Alan"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
metadataobjectCustom metadata{}
statusstringOnline status"online"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745554700

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the receiver"cometchat-uid-1"
namestringDisplay name of the receiver"Andrew Joseph"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
metadataobjectCustom metadata{}
statusstringOnline status"offline"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745550000
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_CHAT_API_FAILURE"
messagestringHuman-readable error message"Failed to fetch the requested data."
detailsstringAdditional technical details"An unexpected error occurred while processing the request."

Avoid Threaded Messages in User/Group Conversations

While fetching messages for normal user/group conversations using the MessagesRequest, the threaded messages by default will be a part of the list of messages received. Use hideReplies = true on the MessagesRequestBuilder to exclude threaded messages from the list.
String UID = "cometchat-uid-1";

MessagesRequest messageRequest = (MessagesRequestBuilder()
          ..uid = UID
          ..hideReplies = true
          ..limit = 50).build();

messageRequest.fetchNext(onSuccess: (List<BaseMessage> list) {
         debugPrint("Message fetching Successful");
      }, onError: (CometChatException e) {
        debugPrint("Message fetching failed with exception: ${e.message}");
      });
On Success — A List<BaseMessage> containing the fetched messages excluding threaded replies (each item is a BaseMessage object):BaseMessage Object:
ParameterTypeDescriptionSample Value
idnumberUnique message ID603
metadataobjectCustom metadata attached to the message{}
receiverobjectReceiver user objectSee below ↓
editedBystringUID of the user who edited the messagenull
conversationIdstringUnique conversation identifier"cometchat-uid-1_user_cometchat-uid-2"
sentAtnumberEpoch timestamp when the message was sent1745554729
receiverUidstringUID of the receiver"cometchat-uid-1"
typestringType of the message"text"
readAtnumberEpoch timestamp when the message was read0
deletedBystringUID of the user who deleted the messagenull
deliveredAtnumberEpoch timestamp when the message was delivered0
deletedAtnumberEpoch timestamp when the message was deleted0
replyCountnumberNumber of replies to this message0
senderobjectSender user objectSee below ↓
receiverTypestringType of the receiver"user"
editedAtnumberEpoch timestamp when the message was edited0
parentMessageIdnumberID of the parent message (for threads)0
readByMeAtnumberEpoch timestamp when read by the current user0
categorystringMessage category"message"
deliveredToMeAtnumberEpoch timestamp when delivered to the current user0
updatedAtnumberEpoch timestamp when the message was last updated1745554729
unreadRepliesCountnumberCount of unread replies0
quotedMessageIdnumberID of the quoted messagenull
quotedMessageobjectThe quoted message objectnull

sender Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the sender"cometchat-uid-2"
namestringDisplay name of the sender"George Alan"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
metadataobjectCustom metadata{}
statusstringOnline status"online"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745554700

receiver Object:
ParameterTypeDescriptionSample Value
uidstringUnique identifier of the receiver"cometchat-uid-1"
namestringDisplay name of the receiver"Andrew Joseph"
linkstringProfile linknull
avatarstringAvatar URL"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"
metadataobjectCustom metadata{}
statusstringOnline status"offline"
rolestringUser role"default"
statusMessagestringStatus messagenull
tagsarrayUser tags[]
hasBlockedMebooleanWhether this user has blocked the current userfalse
blockedByMebooleanWhether the current user has blocked this userfalse
lastActiveAtnumberEpoch timestamp of last activity1745550000
ParameterTypeDescriptionSample Value
codestringError code identifier"ERR_CHAT_API_FAILURE"
messagestringHuman-readable error message"Failed to fetch the requested data."
detailsstringAdditional technical details"An unexpected error occurred while processing the request."
The response is a list of BaseMessage objects, excluding any messages that are replies within a thread. Only top-level messages in the conversation are returned.

Next Steps

Send Messages

Learn how to send text, media, and custom messages

Receive Messages

Handle incoming messages in real-time

Reactions

Add emoji reactions to messages

Message Structure

Understand message types and hierarchy