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
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:- TextMessage Object
- MediaMessage Object
- CustomMessage Object
- User Object
- Group Object
- CometChatException Object
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 thesetLimit() 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.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| limit | Int | 30 |
Messages for a user conversation
In other words, how do I fetch messages between me and any user This can be achieved using thesetUID() method. This method takes the UID of the user with whom the conversation is to be fetched.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-3" |
| limit | Int | 50 |
Messages for a group conversation
In other words, how do I fetch messages for any group conversation You can achieve this using thesetGUID() method. This method takes the GUID of a group for which the conversations are to be fetched.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| guid | String | "cometchat-guid-1" |
| limit | Int | 50 |
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 thesetMessageId() 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.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-3" |
| messageID | Int | 37797 |
| limit | Int | 30 |
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 thesetTimestamp() method. This method takes the Unix timestamp as input.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-3" |
| timeStamp | Int | 1771848542 |
| limit | Int | 30 |
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 thesetUnread() method.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-3" |
| unread | Bool | true |
| limit | Int | 30 |
Messages for multiple categories
In other words, how do I fetch messages belonging to multiple categories For this, you will have to use thesetCategories() method. This method accepts a list of categories.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
categories (
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-3" |
| limit | Int | 30 |
[String]):| Index | Type | Value |
|---|---|---|
| 0 | String | "message" |
Messages for multiple types
In other words, how do I fetch messages belonging to multiple types This can be easily achieved using thesetTypes() method. This method accepts a list of types.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
types (
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-3" |
| limit | Int | 30 |
[String]):| Index | Type | Value |
|---|---|---|
| 0 | String | "text" |
Messages by tags
In other words, how do I fetch messages belonging to specific tags In order to do this, you can use thesetTags() method. This method accepts a list of tags.
- Swift
Sample Payloads
Sample Payloads
- Request
- Success Response
- Error Response
Method:
tags (
MessagesRequest.fetchPrevious()| Parameter | Type | Value |
|---|---|---|
| uid | String | "cometchat-uid-3" |
| limit | Int | 30 |
[String]):| Index | Type | Value |
|---|---|---|
| 0 | String | "important" |
Common Error Codes
| Error Code | Description | Resolution |
|---|---|---|
ERROR_USER_NOT_LOGGED_IN | User is not logged in | Call CometChat.login() first |
ERR_SDK_NOT_INITIALIZED | SDK not initialized | Call CometChat.init() first |
ERR_NETWORK_ERROR | Network connectivity issue | Check internet connection and retry |
ERR_INVALID_UID | Invalid user UID | Verify the UID exists |
ERR_INVALID_GUID | Invalid group GUID | Verify the GUID exists |