Skip to main content
Quick Reference for AI Agents & Developers
// Build filtered message request
val messagesRequest = MessagesRequestBuilder()
    .setUID("user_uid")                    // User conversation
    .setLimit(50)                          // Max 100 per request
    .setCategories(listOf("message"))      // Filter by category
    .setTypes(listOf("text", "image"))     // Filter by type
    .setUnread(true)                       // Only unread messages
    .hideDeletedMessages(true)             // Exclude deleted
    .hideReplies(true)                     // Exclude threaded messages
    .build()

// Fetch messages
messagesRequest.fetchNext(callback)
messagesRequest.fetchPrevious(callback)
The MessagesRequest class as you must be familiar with helps you to fetch messages based on the various parameters provided to it. This document will help you understand better the various options that are available using the MessagesRequest class. The MessagesRequest class is designed using the Builder design pattern. In order to obtain an object of the MessagesRequest class, you will have to make use of the MessagesRequestBuilder class in the MessagesRequest class. The MessagesRequestBuilder class allows you to set various parameters to the MessagesRequest class based on which the messages are fetched. Steps to generate an object of the MessagesRequest class:
  1. Create an object of the MessagesRequestBuilder class.
  2. Set all the parameters you wish to set.
  3. Call the build() method of the MessagesRequestBuilder class to get an object of the MessagesRequest class.
Once you have an object of the MessagesRequest class, you can call either the fetchNext() method or the fetchPrevious() method using the object.
  1. fetchNext() - Calling this method will return the messages after the specified parameters.
  2. fetchPrevious() - Calling this method will give you messages before the specified parameters.
Since messages are obtained in a paginated manner, a maximum of 100 messages can be pulled in a single iteration. Calling the fetchPrevious()/fetchNext() method on the same MessagesRequest object will get you the next set of messages. Now that you are clear how to use the MessagesRequest class, below are the various options available:

Number of messages fetched

How do I set the number of messages fetched in a single iteration? To achieve this, use the setLimit() method. This method takes an integer value as the input and informs the SDK to fetch the specified number of messages in one iteration. The maximum number of messages that can be fetched in one go is 100.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
.setLimit(50)
.setUID(UID)
.build();

Messages for a user conversation

How do I fetch messages between me and any user? This can be achieved using the setUID() method. This method takes the UID of the user with whom the conversation is to be fetched. When a valid UID is passed, the SDK returns all the messages that are a part of the conversation between the logged-in user and the UID that has been specified.
MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
.setUID("cometchat-uid-1")
.setLimit(50)
.build();

Messages for a group conversation

How do I fetch messages for any group conversation? You can achieve this using the setGUID() method. This method takes the GUID of a group for which the conversations are to be fetched. Passing a valid GUID to this method returns all the messages that are a part of the group conversation. Note that the logged-in user must be a member of the group to fetch the messages for that group. MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder() .setGUID(“cometchat-guid-1”) .setLimit(50) .build();
MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
.setGUID("cometchat-guid-1")
.setLimit(50)
.build();

Messages before/after a message

How do I fetch messages before or after a particular message? This can be achieved using the setMessageId() method. This method takes the message-id as input and provides messages only after/before the message-id based on whether the fetchNext() or fetchPrevious() method is triggered.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setMessageId(100)
  .setLimit(50)
  .setUID(UID)
  .build();
This method can be used along with setUID() or setGUID() methods to fetch messages after/before any specific message-id for a particular user/group conversation.

Messages before/after a given time

How do I fetch messages before or after a particular date or time? You can easily achieve this using the setTimestamp() method. This method takes the Unix timestamp as input and provides messages only after/before the timestamp based on whether fetchNext() or fetchPrevious() method is triggered.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setTimestamp(1512687363)
  .setLimit(50)
  .setUID(UID)
  .build();
This method can be used along with setUID() or setGUID() methods to fetch messages after/before any specific date or time for a particular user/group conversation.

Unread messages

How do I fetch unread messages? This can easily be achieved by setting the unread flag to true. For this, use the setUnread() method. This method takes a boolean value as input. If the value is set to true, the SDK returns just the unread messages.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setUnread(true)
  .setLimit(50)
  .setUID(UID)
  .build();
This method along with setGUID() or setUID() can be used to fetch unread messages for a particular group or user conversation respectively.

Exclude messages from blocked users

How do I fetch messages excluding the messages from the users I have blocked? This can be easily achieved using the hideMessagesFromBlockedUsers() method. This method accepts a boolean value which determines if the messages from users blocked by the logged-in user need to be a part of the fetched messages. If the value is set to true, the messages will be hidden and won’t be a part of the messages fetched. The default value is false, meaning if this method is not used, the messages from blocked users will be included in the fetched messages.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .hideMessagesFromBlockedUsers(true)
  .setUID(UID)
  .setLimit(50)
  .build(); 
This method can be used to hide the messages by users blocked by logged in user in groups that both the members are a part of.

Updated and received messages

How do I fetch messages that have been received or updated after a particular date or time? This method accepts a Unix timestamp value and returns all the messages that have been updated and the ones that have been sent/received after the specified time. The messages updated could mean the messages that have been marked as read/delivered or if the messages are edited or deleted.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setUpdatedAfter(1514321321)
  .setLimit(50)
  .setUID(UID)
  .build();
This can be useful in finding the messages that have been received or updated after a certain time. Can prove very useful if you are saving the messages locally and would like to know the messages that have been updated or received after the last message available in your local databases.

Updated messages only

How do I fetch messages that have been updated after a particular date or time? This can be achieved easily by setting the updatesOnly parameter to true. To do so, use the updatesOnly() method. This method takes a boolean input and can be used with the setUpdatedAfter() method to get just the updated messages and not the messages sent/received after the specified time. This method cannot be used independently and always needs to be used with the setUpdatedAfter() method.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setUpdatedAfter(1514321321)
  .updatesOnly(true)
  .setLimit(50)
  .setUID(UID)
  .build();

Messages for multiple categories

How do I fetch messages belonging to multiple categories? We recommend before trying this, you refer to the Message structure and hierarchy guide to get familiar with the various categories of messages. For this, use the setCategories() method. This method accepts a list of categories. This tells the SDK to fetch messages only belonging to these categories.
String GUID = "cometchat-guid-1";
List<String> categories = new ArrayList<>();
categories.add("message");
categories.add("custom");

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setCategories(categories)
  .setLimit(50)
  .setGUID(GUID)
  .build();
The above snippet will help you get only the messages belonging to the message and custom category. This can also be used to disable certain categories of messages like call and action. This along with setGUID() and setUID() can help display only the messages you wish to display avoiding the other category of messages.

Messages for multiple types

How do I fetch messages belonging to multiple types? We recommend before trying this, you refer to the Message structure and hierarchy guide to get familiar with the various types of messages. This can be easily achieved using the setTypes() method. This method accepts a list of types. This tells the SDK to fetch messages only belonging to these types.
String UID = "cometchat-uid-1";
List<String> categories = new ArrayList<>();
categories.add("message");
List<String> types = new ArrayList<>();
types.add("image");
types.add("video");
types.add("audio");
types.add("file");

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setCategories(categories)
  .setTypes(types)
  .setLimit(50)
  .setUID(UID)
  .build();
Using the above code snippet, you can fetch all the media messages. This along with setUID() or setGUID() can be used to fetch media messages for any particular conversation. This can be useful in many other scenarios as well.

Messages for a specific thread

How do I fetch messages that are a part of a thread and not directly a user/group conversation? This can be done using the setParentMessageId() method. This method needs to be used when you have implemented threaded conversations in your app. This method returns the messages only belonging to the thread with the specified parent Id.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .setParentMessageId(100)
  .build();
The above code snippet returns the messages that belong to the thread with parent id 100.

Hide threaded messages in user/group conversations

How do I exclude threaded messages from the normal user/group conversations? To do this, use the hideReplies() method. This method is also related to threaded conversations. This method takes a boolean as input. This boolean when set to true ensures that the messages that belong to threads are not fetched. If set to false, which is also the default value, the messages belonging to the threads will also be fetched along with other messages.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .hideReplies(true)
  .setUID(UID)
  .build();

Hide deleted messages in user/group conversations

How do I exclude deleted messages from a user/group conversation? To do this, use the hideDeletedMessages() method. This method takes a boolean as input. This boolean when set to true ensures that the deleted messages are not fetched. If set to false, which is also the default value, the deleted messages will also be fetched along with other messages.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .hideDeletedMessages(true)
  .build();

Hide quoted messages in user/group conversations

How do I exclude quoted messages in a user/group conversation? To do this, use the hideQuotedMessages() method. This method takes a boolean as input. This boolean when set to true ensures that the quoted messages are not fetched. If set to false, which is also the default value, the quoted messages will also be fetched along with other messages.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .hideQuotedMessages(true)
  .build();

Messages by tags

How do I fetch messages belonging to specific tags? To do this, use the setTags() method. This method accepts a list of tags. This tells the SDK to fetch messages only belonging to these tags.
String UID = "cometchat-uid-1";
List<String> tags = new ArrayList<>();
tags.add("pinned");

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .setTags(tags)
  .build();

Messages with tags

How do I fetch messages with the tags information? To do this, use the withTags() method. This method accepts a boolean as input. When set to true, the SDK fetches messages along with the tags information. When set to false, the SDK does not fetch tags information associated with messages. The default value for this parameter is false.
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .withTags(true)
  .build();
In other words, as a logged-in user, how do I fetch messages that contains links? In order to do this, you can use the hasLinks() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have links in the text. The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .hasLinks(true)
  .build();

Messages with attachments

In other words, as a logged-in user, how do I fetch messages that contains attachments? In order to do this, you can use the hasAttachments() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have attachments (image, audio, video or file). The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .hasAttachments(true)
  .build();

Messages with reactions

In other words, as a logged-in user, how do I fetch messages that contains reactions? In order to do this, you can use the hasReactions() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have reactions. The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .hasReactions(true)
  .build();

Messages with mentions

In other words, as a logged-in user, how do I fetch messages that contains mentions? In order to do this, you can use the hasMentions() method. This method accepts boolean as input. When set to true , the SDK will fetch messages which have mentions. The default value for this parameter is false.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
String UID = "cometchat-uid-1";

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .hasMentions(true)
  .build();

Messages with particular user mentions

In other words, as a logged-in user, how do I fetch messages that mentions specific users? In order to do this, you can use the setMentionedUIDs() method. This method accepts an array of UIDs as input. When set, the SDK will fetch messages which have the mentions of the UIDs passed.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
String UID = "cometchat-uid-1";
List<String> mentionedUIDs = new ArrayList<>();
mentionedUIDs.add("cometchat-uid-1");

MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
  .setLimit(50)
  .setUID(UID)
  .setMentionedUIDs(mentionedUIDs)
  .build();

Messages with specific attachment types

In other words, as a logged-in user, how do I fetch messages that contain specific types of attachments? In order to do this, you can use the setAttachmentTypes() method. This method accepts an array of CometChat.AttachmentType ENUM values as input. When provided, the SDK will fetch only those messages that include attachments of the specified types (such as image, audio, video, or file).
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Custom plans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
String UID = "cometchat-uid-1";
  List<AttachmentType> attachmentTypes = new ArrayList<>();
  attachmentTypes.add(AttachmentType.IMAGE);
  attachmentTypes.add(AttachmentType.FILE);

  MessagesRequest messagesRequest = new MessagesRequest.MessagesRequestBuilder()
      .setLimit(50)
      .setUID("UID")
      .setAttachmentTypes(attachmentTypes)
      .build();

Best Practices

Set a reasonable limit (20-50 messages) using setLimit() to balance performance and user experience. Fetch more messages as users scroll using fetchNext() or fetchPrevious().
Combine multiple filters to create specific views (e.g., unread media messages, messages with links, messages from specific time ranges). This reduces client-side filtering.
Store filtered message results in local storage to provide offline access and reduce API calls. Refresh the cache when new messages arrive via listeners.
Use setUpdatedAfter() with updatesOnly(true) to efficiently sync messages that have been edited, deleted, or marked as read/delivered since your last fetch.
For advanced filtering (links, attachments, reactions, mentions), ensure “Conversation & Advanced Search” is enabled in your CometChat Dashboard under Chats → Settings → General Configuration.

Next Steps