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.
AI Integration Quick Reference
Field Value Key Classes CometChatMessageDelegate, MessagesRequest.MessageRequestBuilderKey Methods onTextMessageReceived(), onMediaMessageReceived(), onCustomMessageReceived(), fetchPrevious(), fetchNext()Delegate CometChat.messagedelegate = selfPrerequisites SDK initialized, user logged in
Receiving messages with CometChat has two parts:
Adding a real-time listener to receive messages while your app is running
Fetching unread or historical messages when your app starts up or the user scrolls back
Real-Time Messages
Register a CometChatMessageDelegate to receive incoming messages as they arrive. Each callback receives the specific message subclass — TextMessage , MediaMessage , or CustomMessage .
extension ViewController : CometChatMessageDelegate {
func onTextMessageReceived ( textMessage : TextMessage) {
print ( "TextMessage received successfully: " + textMessage. stringValue ())
}
func onMediaMessageReceived ( mediaMessage : MediaMessage) {
print ( "MediaMessage received successfully: " + mediaMessage. stringValue ())
}
func onCustomMessageReceived ( customMessage : CustomMessage) {
print ( "CustomMessage received successfully: " + customMessage. stringValue ())
}
}
@interface ViewController ()<CometChatMessageDelegate>
@end
@implementation ViewController
- ( void ) viewDidLoad {
[ super viewDidLoad ];
CometChat . messagedelegate = self ;
}
- ( void ) onTextMessageReceivedWithTextMessage: (TextMessage * ) textMessage {
NSLog ( @"TextMessage received successfully: %@ " ,[textMessage stringValue ]);
}
- ( void ) onMediaMessageReceivedWithMediaMessage: (MediaMessage * ) mediaMessage {
NSLog ( @"MediaMessage received successfully: %@ " ,[mediaMessage stringValue ]);
}
- ( void ) onCustomMessageReceivedWithCustomMessage: (CustomMessage * ) customMessage {
NSLog ( @"CustomMessage received: %@ " ,[customMessage stringValue ]);
}
@end
Don’t forget to set your view controller as the delegate in viewDidLoad():
CometChat. messagedelegate = self
As a sender, you will not receive your own message in a real-time event. However, if a user is logged in on multiple devices, they will receive the event on the other devices.
Missed Messages
Fetch messages that were sent while the app was offline using MessagesRequest with setMessageId() set to the last delivered message ID.
Fetch Missed Messages of a particular one-on-one conversation
let limit = 50
let latestId = CometChat. getLastDeliveredMessageId ()
let UID = "cometchat-uid-2"
let messagesRequest = MessagesRequest. MessageRequestBuilder ()
. set ( messageID : latestId)
. set ( uid : UID)
. set ( limit : limit)
. build ()
messagesRequest. fetchNext ( onSuccess : { (messages) in
for message in messages ! {
if let receivedMessage = (message as? TextMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
} else if let receivedMessage = (message as? MediaMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
}
}
}) { (error) in
print ( "Message receiving failed with error: " + error ! . errorDescription );
}
Fetch Missed Messages of a particular group conversation
let limit = 50
let latestId = CometChat. getLastDeliveredMessageId ()
let GUID = "cometchat-guid-1"
let messagesRequest = MessagesRequest. MessageRequestBuilder ()
. set ( messageID : latestId)
. set ( guid : GUID)
. set ( limit : limit)
. build ()
messagesRequest. fetchNext ( onSuccess : { (messages) in
for message in messages ! {
if let receivedMessage = (message as? TextMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
} else if let receivedMessage = (message as? MediaMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
}
}
}) { (error) in
print ( "Message receiving failed with error: " + error ! . errorDescription );
}
Unread Messages
Fetch unread messages by adding setUnread(true) to the builder. Use fetchPrevious() to retrieve them.
let limit = 50
let UID = "cometchat-uid-2"
let messagesRequest = MessagesRequest. MessageRequestBuilder ()
. set ( unread : true )
. set ( limit : limit)
. set ( uid : UID)
. build ()
messagesRequest. fetchPrevious ( onSuccess : { (messages) in
for message in messages ! {
if let receivedMessage = (message as? TextMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
} else if let receivedMessage = (message as? MediaMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
}
}
}) { (error) in
print ( "Message receiving failed with error: " + error ! . errorDescription );
}
Message History
Fetch the full conversation history using fetchPrevious(). Call it repeatedly on the same request object to paginate.
let limit = 50
let UID = "cometchat-uid-2"
let messagesRequest = MessagesRequest. MessageRequestBuilder ()
. set ( limit : limit)
. set ( uid : UID)
. build ()
messagesRequest. fetchPrevious ( onSuccess : { (messages) in
for message in messages ! {
if let receivedMessage = (message as? TextMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
} else if let receivedMessage = (message as? MediaMessage) {
print ( "Message received successfully. " + receivedMessage. stringValue ())
}
}
}) { (error) in
print ( "Message receiving failed with error: " + error ! . errorDescription );
}
Next Steps
Delivery & Read Receipts Track when messages are delivered and read by recipients
Typing Indicators Show real-time typing status in conversations