Skip to main content
Receiving messages with CometChat has two parts:
  1. Adding a real-time listener to receive messages while your app is running
  2. Fetching missed, unread, or historical messages when your app starts up or the user scrolls back

Real-Time Messages

Register a MessageListener to receive incoming messages as they arrive. For every activity or fragment you wish to receive messages in, register the listener using addMessageListener(). We suggest adding it in the onResume() method.
Remove the listener when you no longer need it. Typically, this can be added in the onPause() method.
Always remove listeners when they’re no longer needed (e.g., in onPause()). Failing to do so can cause memory leaks and duplicate event handling.
As a sender, you will not receive your own message in a real-time message event. However, if a user is logged-in using multiple devices, they will receive an event for their own message in other devices.

Missed Messages

Fetch messages that arrived while your app was offline. Use getLastDeliveredMessageId() to find where you left off, then call fetchNext() to get everything after that point. Call fetchNext() repeatedly on the same request object to paginate.

Unread Messages

Fetch unread messages by adding setUnread(true) to the builder. Use fetchPrevious() to retrieve them.
The list of messages received is in the form of objects of BaseMessage class. A BaseMessage can either be an object of the TextMessage, MediaMessage, CustomMessage, Action or Call class. You can use the instanceOf operator to check the type of object.

Message History

Fetch the full conversation history using fetchPrevious(). Call it repeatedly on the same request object to paginate — useful for implementing upward scrolling.

Search Messages

Add setSearchKeyword() to the builder to filter messages by keyword.

Search Capabilities

By default, search only matches message text. With Conversation & Advanced Search enabled, it also matches file names, mentions, and MIME types.
Conversation & Advanced Search is available on Advanced and Custom plans. Enable it from the CometChat Dashboard under Chats → Settings → General Configuration.

Unread Message Count

CometChat provides several methods to get unread counts at different scopes. All return results via a callback listener with a HashMap mapping IDs to counts. Each method accepts an optional boolean parameter to exclude messages from blocked users.

Single Conversation

All Conversations

Excluding Blocked Users

Pass true as the second argument to any of the methods above to ignore messages from blocked users:

Get Message Details

Use getMessageDetails() to fetch a specific message by its ID. Returns the full message object (TextMessage, MediaMessage, CustomMessage, or other BaseMessage subclass).

Next Steps

Delivery & Read Receipts

Track when messages are delivered and read by recipients

Typing Indicators

Show real-time typing status in conversations

Message History

Advanced filtering options for message history

Send Message

Send text, media, and custom messages

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.Sample BaseMessage Object:
The nested User object (used in sender, receiver, mentionedUsers) contains:
The nested Group object (used in receiver for group messages) contains:
The nested ReactionCount object (used in reactions array) contains: