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.

// Send message in a thread
let msg = TextMessage(receiverUid: "UID", text: "Reply", receiverType: .user)
msg.parentMessageId = 100
CometChat.sendTextMessage(message: msg, onSuccess: { _ in }, onError: { _ in })

// Fetch thread messages
let threadRequest = MessagesRequest.MessageRequestBuilder()
  .setParentMessageId(parentMessageId: 100).set(limit: 30).build()
threadRequest.fetchPrevious(onSuccess: { _ in }, onError: { _ in })

// Exclude thread replies from main conversation
let mainRequest = MessagesRequest.MessageRequestBuilder()
  .set(uid: "UID").set(limit: 30).hideReplies(hide: true).build()
Threaded messages (or threads) are messages started from a particular parent message. Each thread is attached to a parent message.

Send Message in a Thread

Any message type (Text, Media, or Custom) can be sent in a thread. Set the parentMessageId property to indicate which thread the message belongs to.

Send Text Message in Thread

let receiverID = "cometchat-uid-2"
let text = "Hello"

let textMessage = TextMessage(receiverUid: receiverID, text: text, receiverType: .user)
textMessage.parentMessageId = 38208

CometChat.sendTextMessage(message: textMessage, onSuccess: { (message) in
  print("TextMessage sent successfully. " + message.stringValue())
}) { (error) in
  print("TextMessage sending failed with error: " + error!.errorDescription);
}

Send Media Message in Thread

let receiverID = "cometchat-uid-2"
let fileUrl = "https://data-us.cometchat.io/assets/images/avatars/ironman.png"

let mediaMessage = MediaMessage(receiverUid: receiverID, fileurl: fileUrl, messageType: .image, receiverType: .user)
mediaMessage.parentMessageId = 38208

CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
  print("MediaMessage sent successfully. " + message.stringValue())
}) { (error) in
  print("MediaMessage sending failed with error: " + error!.errorDescription);
}

Send Custom Message in Thread

let receiverID = "cometchat-uid-2"
let customData: [String: Any] = ["emoji": "👍", "replyType": "reaction"]

let customMessage = CustomMessage(receiverUid: receiverID, receiverType: .user, customData: customData, type: "thread_reaction")
customMessage.parentMessageId = 38208

CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in
  print("CustomMessage sent successfully. " + message.stringValue())
}) { (error) in
  print("CustomMessage sending failed with error: " + error!.errorDescription);
}

Receiving Real-Time Messages

Use CometChatMessageDelegate to receive real-time thread messages. Check if the received message belongs to the active thread using parentMessageId.
var activeThreadId = 38208

extension ViewController: CometChatMessageDelegate {

  func onTextMessageReceived(textMessage: TextMessage) {
    if textMessage.parentMessageId == activeThreadId {
      print("TextMessage received in thread: " + textMessage.stringValue())
    }
  }

  func onMediaMessageReceived(mediaMessage: MediaMessage) {
    if mediaMessage.parentMessageId == activeThreadId {
      print("MediaMessage received in thread: " + mediaMessage.stringValue())
    }
  }

  func onCustomMessageReceived(customMessage: CustomMessage) {
    if customMessage.parentMessageId == activeThreadId {
      print("CustomMessage received in thread: " + customMessage.stringValue())
    }
  }
}

Fetch all the messages for any particular thread

Use MessagesRequestBuilder with setParentMessageId() to fetch messages belonging to a specific thread. The fetchPrevious() method returns an array of BaseMessage objects representing thread replies.
let messagesRequest = MessagesRequest.MessageRequestBuilder()
  .setParentMessageId(parentMessageId: 38208)
  .set(limit: 50)
  .build()

messagesRequest.fetchPrevious(onSuccess: { (messages) in
  for message in messages! {
    if let textMessage = message as? TextMessage {
      print("Text Message: " + textMessage.stringValue())
    } else if let mediaMessage = message as? MediaMessage {
      print("Media Message: " + mediaMessage.stringValue())
    } else if let customMessage = message as? CustomMessage {
      print("Custom Message: " + customMessage.stringValue())
    }
  }
}) { (error) in
  print("Messages fetching failed with error: " + error!.errorDescription)
}

Avoid Threaded Messages in User/Group Conversations

Use hideReplies(hide: true) to exclude threaded messages when fetching messages for a conversation. The response is an array of BaseMessage objects, excluding any messages that are replies within a thread.
let messagesRequest = MessagesRequest.MessageRequestBuilder()
  .set(uid: "cometchat-uid-2")
  .set(limit: 50)
  .hideReplies(hide: true)
  .build()

messagesRequest.fetchPrevious(onSuccess: { (messages) in
  for message in messages! {
    print("Message ID: \(message.id), parentMessageId: \(message.parentMessageId)")
  }
}) { (error) in
  print("Messages fetching failed with error: " + error!.errorDescription)
}

Next Steps

Send Messages

Send text, media, and custom messages to users and groups

Receive Messages

Listen for incoming messages in real-time and fetch missed messages

Reactions

Add emoji reactions to messages

Message Filtering

Advanced message filtering with RequestBuilder