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.

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

Set the number of messages to fetch per request using setLimit(). Maximum is 100.
let messagesRequest: CometChat.MessagesRequest =
  new CometChat.MessagesRequestBuilder().setLimit(50).build();

Messages for a user conversation

Use setUID() to fetch messages between the logged-in user and a specific user.
let UID = "UID";
let messagesRequest: CometChat.MessagesRequest =
  new CometChat.MessagesRequestBuilder().setUID(UID).setLimit(50).build();
When messages are fetched successfully, the response includes an array of message objects.

Messages for a group conversation

Use setGUID() to fetch messages from a group. The logged-in user must be a member of the group.
let GUID: string = "GUID";
let messagesRequest: CometChat.MessagesRequest =
  new CometChat.MessagesRequestBuilder().setGUID(GUID).setLimit(50).build();
When messages are fetched successfully, the response includes an array of message objects.

Messages before/after a message

Use setMessageId() to fetch messages before or after a specific message ID. Use fetchNext() to get messages after, or fetchPrevious() to get messages before.
let UID: string = "UID",
  messageId: number = 1,
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setMessageId(messageId)
      .setLimit(limit)
      .build();
This method can be combined with setUID() or setGUID() to fetch messages around a specific message in a conversation.

Messages before/after a given time

Use setTimestamp() with a Unix timestamp to fetch messages before or after a specific time.
let UID: string = "UID",
  timestamp: number = 1602221371,
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setTimestamp(timestamp)
      .setLimit(limit)
      .build();
This method can be combined with setUID() or setGUID() to fetch messages around a specific time in a conversation.

Unread messages

Use setUnread(true) to fetch only unread messages.
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setUnread(true)
      .setLimit(limit)
      .build();
Combine with setGUID() or setUID() to fetch unread messages for a specific conversation.

Exclude messages from blocked users

Use hideMessagesFromBlockedUsers(true) to exclude messages from users you’ve blocked. Default is false.
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .hideMessagesFromBlockedUsers(true)
      .setLimit(limit)
      .build();
This also works in group conversations where both users are members.

Updated and received messages

Use setUpdatedAfter() with a Unix timestamp to fetch messages that were sent or updated after a specific time. Updated messages include those marked as read/delivered, edited, or deleted.
let UID: string = "UID",
  limit: number = 30,
  timestamp: string = "1602221371",
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setUpdatedAfter(timestamp)
      .setLimit(limit)
      .build();
Useful for syncing messages with a local database — fetch only what’s changed since your last sync.

Updated messages only

Use updatesOnly(true) with setUpdatedAfter() to fetch only updated messages (not newly received ones). This method must be used together with setUpdatedAfter().
let UID: string = "UID",
  limit: number = 30,
  timestamp: string = "1602221371",
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setUpdatedAfter(timestamp)
      .updatesOnly(true)
      .setLimit(limit)
      .build();
When messages are fetched successfully, the response includes only messages that have been updated (edited, deleted, read/delivered status changed) after the specified timestamp.

Messages for multiple categories

Use setCategories() with an array of category names to filter by message category. See Message structure and hierarchy for available categories.
let UID: string = "UID",
  limit: number = 30,
  categories: Array<String> = ["message", "custom"],
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setCategories(categories)
      .setLimit(limit)
      .build();
The above snippet fetches only messages in the message and custom categories. Use this to exclude categories like call and action.

Messages for multiple types

Use setTypes() with an array of type names to filter by message type. See Message structure and hierarchy for available types.
let UID: string = "UID",
  limit: number = 30,
  categories: Array<String> = ["message", "custom"],
  types: Array<String> = ["image", "video", "audio", "file"],
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setCategories(categories)
      .setTypes(types)
      .setLimit(limit)
      .build();
The above snippet fetches all media messages (image, video, audio, file).

Messages for a specific thread

Use setParentMessageId() to fetch messages belonging to a specific thread.
let UID: string = "UID",
  limit: number = 30,
  messageId: number = 1,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setLimit(limit)
      .setParentMessageId(messageId)
      .build();
The above code returns messages belonging to the thread with the specified parent message ID.

Hide threaded messages in user/group conversations

Use hideReplies(true) to exclude threaded messages from the main conversation. Default is false.
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setLimit(limit)
      .hideReplies(true)
      .build();

Hide deleted messages in user/group conversations

Use hideDeletedMessages(true) to exclude deleted messages. Default is false.
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setLimit(limit)
      .hideDeletedMessages(true)
      .build();

Hide quoted messages in user/group conversations

Use hideQuotedMessages(true) to exclude quoted messages. Default is false.
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setLimit(limit)
      .hideQuotedMessages(true)
      .build();

Messages by tags

Use setTags() with an array of tag names to fetch only messages with those tags.
let UID: string = "UID",
  limit: number = 30,
  tags: Array<String> = ["starredMessage"],
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setUID(UID)
      .setLimit(limit)
      .setTags(tags)
      .build();

Messages with tags

Use withTags(true) to include tag information in the response. Default is false.
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setGUID(UID)
      .setLimit(limit)
      .withTags(true)
      .build();
When withTags(true) is set, each message’s tags field will be populated. Access tags using getTags().
Additional FieldGetterReturn TypeDescription
tagsgetTags()string[]Tags associated with the message
Use hasLinks(true) to fetch only messages containing links. Default 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)
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setGUID(UID)
      .setLimit(limit)
      .hasLinks(true)
      .build();

Messages with attachments

Use hasAttachments(true) to fetch only messages with attachments (image, audio, video, or file). Default 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)
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setGUID(UID)
      .setLimit(limit)
      .hasAttachments(true)
      .build();
The response contains media message objects with attachment details including file metadata and thumbnail URLs.

Messages with reactions

Use hasReactions(true) to fetch only messages that have reactions. Default 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)
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setGUID(UID)
      .setLimit(limit)
      .hasReactions(true)
      .build();
The response contains message objects with reactions. Each message’s data object includes a reactions array.

Messages with mentions

Use hasMentions(true) to fetch only messages that contain mentions. Default 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)
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setGUID(UID)
      .setLimit(limit)
      .hasMentions(true)
      .build();
The response contains text message objects with mentions. Each message has a mentionedUsers array, a mentionedMe boolean, and a data.mentions object.

Messages with particular user mentions

Use setMentionedUIDs() with an array of UIDs to fetch messages that mention specific users.
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)
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setGUID(UID)
      .setLimit(limit)
      .setMentionedUIDs(["UID"])
      .build();
The response contains text message objects that mention the specified users.

Messages with specific attachment types

Use setAttachmentTypes() with an array of CometChat.AttachmentType values to fetch messages with specific attachment types.
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)
let UID: string = "UID",
  limit: number = 30,
  messagesRequest: CometChat.MessagesRequest =
    new CometChat.MessagesRequestBuilder()
      .setGUID(UID)
      .setLimit(limit)
      .setAttachmentTypes([CometChat.AttachmentType.IMAGE, CometChat.AttachmentType.VIDEO])
      .build();
The response contains media message objects filtered to the specified attachment types.

Next Steps

Send Messages

Send text, media, and custom messages

Receive Messages

Listen for incoming messages in real-time

Message Structure

Understand message categories, types, and hierarchy

Threaded Messages

Work with threaded conversations