Skip to main content
Quick Reference for AI Agents & Developers
  • Filter by type: .set(types:) — e.g., ["text", "image"]
  • Filter by category: .set(categories:) — e.g., ["message", "action"]
  • Filter by tags: .setTags(_:), .withTags(true)
  • Filter by sender: .set(uid:) for specific user’s messages
  • Exclude: .hideReplies(true), .hideDeletedMessages(true)
  • Related: Receive Message · Message Structure · Messaging Overview
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.

Object Structures

For complete object structure definitions, see Send Message - Object Structures:

Number of messages fetched

In other words, how do I set the number of messages fetched in a single iteration To achieve this, you can 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.
var limit = 50;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(limit: limit)
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
limitInt30

Messages for a user conversation

In other words, 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.
var limit = 30;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(uid: "cometchat-uid-1")
.set(limit: limit)
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-3"
limitInt50

Messages for a group conversation

In other words, 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.
var limit = 30;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(guid: "cometchat-guid-1")
.set(limit: limit)
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
guidString"cometchat-guid-1"
limitInt50
If none of the above two methods setUID() and setGUID() is used, all the messages for the logged-in user will be fetched. This means that messages from all the one-on-one and group conversations which the logged-in user is a part of will be returned.

Messages before/after a message

In other words, 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 if the fetchNext() or fetchPrevious() method is triggered.
var limit = 30;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(messageID: 100)
.set(limit: limit)
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-3"
messageIDInt37797
limitInt30

Messages before/after a given time

In other words, 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.
var limit = 30;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(timeStamp: 151268736300)
.set(limit: limit)
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-3"
timeStampInt1771848542
limitInt30

Unread messages

In other words, how do I fetch unread messages This can easily be achieved using setting the unread flag to true. For this, you need to use the setUnread() method.
var limit = 30;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(unread: true)
.set(limit: limit)
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-3"
unreadBooltrue
limitInt30

Messages for multiple categories

In other words, how do I fetch messages belonging to multiple categories For this, you will have to use the setCategories() method. This method accepts a list of categories.
var limit = 30;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(limit: limit)
.set(categories: ["message","call","action"])
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-3"
limitInt30
categories ([String]):
IndexTypeValue
0String"message"

Messages for multiple types

In other words, how do I fetch messages belonging to multiple types This can be easily achieved using the setTypes() method. This method accepts a list of types.
var limit = 30;
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.set(limit: limit)
.set(types: ["text","image"])
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-3"
limitInt30
types ([String]):
IndexTypeValue
0String"text"

Messages by tags

In other words, how do I fetch messages belonging to specific tags In order to do this, you can use the setTags() method. This method accepts a list of tags.
var limit = 30
let tags = ["pinned"]
let messagesRequest = MessagesRequest.MessageRequestBuilder()
.setLimit(50)
.setTags(tags)
.build();
Method: MessagesRequest.fetchPrevious()
ParameterTypeValue
uidString"cometchat-uid-3"
limitInt30
tags ([String]):
IndexTypeValue
0String"important"

Common Error Codes

Error CodeDescriptionResolution
ERROR_USER_NOT_LOGGED_INUser is not logged inCall CometChat.login() first
ERR_SDK_NOT_INITIALIZEDSDK not initializedCall CometChat.init() first
ERR_NETWORK_ERRORNetwork connectivity issueCheck internet connection and retry
ERR_INVALID_UIDInvalid user UIDVerify the UID exists
ERR_INVALID_GUIDInvalid group GUIDVerify the GUID exists