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
Key MethodssendTextMessage(), sendMediaMessage(), sendCustomMessage()
Receiver Types.user, .group
Message Types.text, .image, .video, .audio, .file, custom
PrerequisitesSDK initialized, user logged in
CometChat supports three types of messages:
TypeMethodUse Case
TextsendTextMessage()Plain text messages
MediasendMediaMessage()Images, videos, audio, files
CustomsendCustomMessage()Location, polls, or any JSON data
You can also send metadata along with a text, media or custom 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

Send a text message using CometChat.sendTextMessage() with a TextMessage object.
let receiverID = "cometchat-uid-2"
let text = "Hello"

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

CometChat.sendTextMessage(message: textMessage, onSuccess: { (message) in
  print("TextMessage sent successfully. " + message.stringValue())
}) { (error) in
  print("TextMessage sending failed with error: " + error!.errorDescription);
}
The TextMessage class constructor takes the following parameters:
ParameterDescriptionRequired
receiverIDUID of the user or GUID of the group receiving the messageYes
textThe text message contentYes
receiverType.user or .groupYes
On success, sendTextMessage() returns a TextMessage object containing all information about the sent message.

Add Metadata

Send custom data along with a text message using the metaData property:
let metadata = ["latitude":"50.6192171633316","longitude":"-72.68182268750002"];
textMessage.metaData = metadata;

Add Tags

let tags = ["pinned"]
textMessage.tags = tags

Quote a Message

textMessage.quotedMessageId = 140
textMessage.quotedMessage = // Pass the BaseMessage object you want to quote

Media Message

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

Upload a File

let receiverid = "cometchat-uid-2"
let mediaUrl = "file:///Library/Developer/CoreSimulator/.../image.jpg"

let mediaMessage = MediaMessage(receiverUid: receiverid, fileurl: mediaUrl, messageType: .image, receiverType: .user)

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

Send a URL

Use the Attachment class to send media hosted on your server or cloud storage:
let receiverid = "cometchat-uid-2"
let mediaUrl = "https://pngimg.com/uploads/mario/mario_PNG125.png"

let mediaMessage = MediaMessage(receiverUid: receiverid, fileurl: "", messageType: .image, receiverType: .user)
mediaMessage.attachment = Attachment(fileName: "FileName", fileExtension: "png", fileMimeType: "image/png", fileUrl: mediaUrl)

CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
  print("MediaMessage sent successfully. " + message.stringValue())
}) { (error) in
  print("MediaMessage sending failed with error: " + error.errorDescription);
}
The MediaMessage class constructor takes the following parameters:
ParameterDescriptionRequired
receiverIDUID of the user or GUID of the groupYes
fileurlFile path or empty string if using URLYes
messageType.image, .video, .audio, or .fileYes
receiverType.user or .groupYes
On success, sendMediaMessage() returns a MediaMessage object.

Add Caption

mediaMessage.caption = "Message Caption"

Add Metadata and Tags

Same as text messages:
mediaMessage.metaData = ["location": "Paris"]
mediaMessage.tags = ["vacation"]

Multiple Attachments in a Media Message

Starting version 3.0.906 & above, the SDK supports sending multiple attachments in a single media message.
let receiverId = "cometchat-uid-1"

let data = try? Data(contentsOf: URL(string: "file:///path/to/image1.jpeg")!)
let data1 = try? Data(contentsOf: URL(string: "file:///path/to/image2.jpeg")!)

var files = [File]()
files.append(File(name: "FileName 1", data: data))
files.append(File(name: "FileName 2", data: data1))

let mediaMessage = MediaMessage(receiverUid: receiverId, files: files, messageType: .file, receiverType: .user)

CometChat.sendMediaMessage(message: mediaMessage, onSuccess: { (message) in
  print("Message sent successfully")
}, onError: { (error) in
  print("Message sending failed: ", error?.errorDescription)
})

Custom Message

Send structured data that doesn’t fit text or media categories — like location coordinates, polls, or game moves.
let receiverid = "cometchat-uid-2"
let customData: [String: Any] = ["customKey": "customData"]

let customMessage = CustomMessage(receiverUid: receiverid, receiverType: .user, customData: customData, type: "Custom Type")

CometChat.sendCustomMessage(message: customMessage, onSuccess: { (message) in
  print("CustomMessage sent successfully. " + message.stringValue())
}) { (error) in
  print("CustomMessage sending failed with error: " + error!.errorDescription);
}
The CustomMessage class constructor takes the following parameters:
ParameterDescriptionRequired
receiverIDUID of the user or GUID of the groupYes
receiverType.user or .groupYes
customDataDictionary with your custom dataYes
typeYour custom type string (e.g., "location", "poll")Yes
On success, sendCustomMessage() returns a CustomMessage object.

Add Tags

customMessage.tags = ["starredMessage"]

Control Conversation Update

By default, custom messages update the conversation’s last message. To prevent this:
customMessage.updateConversation = false

Custom Notification Text

Set custom text for push, email, and SMS notifications:
customMessage.conversationText = "Shared a location"
It is also possible to send interactive messages from CometChat. To learn more, see Interactive Messages.

Next Steps

Receive Messages

Listen for incoming messages in real-time

Edit Message

Edit previously sent messages

Delete Message

Delete sent messages