Skip to main content
Version: v4

Delivery & Read Receipts

Mark Messages as Delivered

In other words, as a recipient, how do I inform the sender that I've received a message?

You can mark the messages for a particular conversation as read using the markAsDelivered() method. This method takes the below parameters as input:

messageIdThe ID of the message above which all the messages for a particular conversation are to be marked as read.
receiverIdIn the case of one-to-one conversation, the sender's sender UID will be the receipt's receiver Id.
In case of group conversation message's receiver Id will be the receipt's receiver Id.
receiverTypeType of the receiver. Could be either of the two values( user or group)
messageSenderThe UID of the sender of the message.

Messages for both user conversation and group conversation can be marked as delivered using this method.

Ideally, you should mark all the messages as read for any conversation when the user opens the chat window for that conversation. This includes two scenarios:

  1. When the list of messages for the conversation is fetched: In this case you need to obtain the last message in the list of messages and pass the message Id of that message to the markAsDelivered() method.
  2. When the user is on the chat window and a real-time message is received: In this case you need to obtain the message Id of the message and pass it to the markAsDelivered() method.
var messageId = 123
var receiverId = "superhero2"
var senderId = "superhero1"
CometChat.markAsDelivered(messageId: messageId, receiverId: receiverId, receiverType: .user, messageSender: senderId)

This method will mark all the messages before the messageId specified, for the conversation with receiverId and receiverType(user/group) as read.

In case you would like to be notified of an error if the receipts fail to go through you can use markAsDelivered() method with the callbacks as shown below:

var messageId = 123
var receiverId = "superhero2"
var senderId = "superhero1"
CometChat.markAsDelivered(messageId: messageId, receiverId: receiverId, receiverType: .user, messageSender: senderId, onSuccess: {
print("markAsDelivered Succces")
}, onError: {(error) in
print("markAsDelivered error message",error?.errorDescription)
})

Another option the CometChat SDK provides is to pass the entire message object to the markAsDelivered() method.

CometChat.markAsDelivered(baseMessage : baseMessage)

In case you would like to be notified of an error if the receipts fail to go through you can use markAsDelivered() method with the callbacks as shown below:

CometChat.markAsDelivered(baseMessage : baseMessage, onSuccess: {
print("markAsDelivered Succces")
}, onError: {(error) in
print("markAsDelivered error message",error?.errorDescription)
})
info

Starting v3, the messages will not be marked delivered internally by the SDK. You will have to use the markAsDelivered() method. You will either have to use one of the above method signatures to mark the messages as delivered.

Mark Messages as Read

In other words, as a recipient, how do I inform the sender I've read a message?

You can mark the messages for a particular conversation as read using the markAsRead() method. This method takes the below parameters as input:

ParameterInformation
messageIdThe ID of the message above which all the messages for a particular conversation are to be marked as read.
receiverIdIn case of one to one conversation message's sender UID will be the receipt's receiver Id.
In case of group conversation message's receiver Id will be the receipts's receiver Id
receiverTypetype of the receiver. Could be either of the two values( user or group)
messageSenderThe UID of the sender of the message.

Messages for both User conversation and Groups can be marked as read using this method.

Ideally, you would like to mark all the messages as read for any conversation when the user opens the chat window for that conversation. This includes two scenarios:

  1. When the list of messages for the conversation is fetched: In this case you need to obtain the last message in the list of messages and pass the message Id of that message to the markAsRead() method.
  2. When the user is on the chat window and a real-time message is received: In this case you need to obtain the message Id of the message and pass it to the markAsRead() method
var messageId = 123
var receiverId = "abc"
var senderId = "xyz"

CometChat.markAsRead(messageId: messageId, receiverId: receiverId, receiverType: .user, messageSender: senderId)

This method will mark all the messages before the messageId specified, for the conversation with receiverId and receiverType(user/group) as read.

In case you would like to be notified of an error if the receipts fail to go through you can use the markAsRead() method with the callbacks as shown below:

var messageId = 123
var receiverId = "superhero2"
var senderId = "superhero1"
CometChat.markAsRead(messageId: messageId, receiverId: receiverId, receiverType: .user, messageSender: senderId, onSuccess: {
print("markAsRead Succces")
}, onError: {(error) in
print("markAsRead error message",error?.errorDescription)
})

Another option the CometChat SDK provides is to pass the entire message object to the markAsRead() method.

CometChat.markAsRead(baseMessage : baseMessage)

In case you would like to be notified of an error if the receipts fail to go through you can use the markAsRead() method with the callbacks as shown below:

var messageId = 123
var receiverId = "superhero2"
var senderId = "superhero1"
CometChat.markAsRead(baseMessage : baseMessage, onSuccess: {
print("markAsRead Succces")
}, onError: {(error) in
print("markAsRead error message",error?.errorDescription)
})
info

Starting v3, the markAsRead() method working with v2.x is deprecated and will not work. You will either have to use one of the above method signatures to mark the messages as read.

Mark Messages as Unread

The Mark as Unread feature allows users to designate specific messages or conversations as unread, even if they have been previously viewed.

This feature is valuable for users who want to revisit and respond to important messages or conversations later, ensuring they don't forget or overlook them.

In other words, how I can mark message as unread?

You can mark the messages for a particular conversation as unread using the markAsUnread() method. This method takes the below parameters as input:

FieldInformation
conversationIdid of the conversation
conversationTypetype of conversation (user/group)
lastMessagelast message in the conversation
conversationWithUser or Group object containing the details
unreadMessageCountunread message count for the conversation
unreadMentionsCountcount of unread mentions in the conversation
lastReadMessageIdID of the last read message in the conversation
let txt = TextMessage(receiverUid: "superhero3", text: "this is unread count test", receiverType: .user)
txt.id = 3 // the message with id 3 will be marked unread. ID is required.
txt.sender = User(uid: "superhero2", name: "Captain America")

CometChat.markAsUnread(baseMessage: txt) {
print("markAsUnread executed successfully: on message with ID: \(txt.id) and text \(txt.text)")
} onError: { error in
print("markAsUnread not executed error: \(error?.errorDescription)")
}

Receive Delivery & Read Receipts

In other words, as a recipient, how do I know when a message I sent has been delivered or read by someone?

Real-time Receipts

Real-time events for messages of a conversation being read or delivered can be obtained in the onMessagesRead() and the onMessagesDelivered() methods of the CometChatMessageDelegate. In order to receive typing indicators, you must add protocol conformance CometChatMessageDelegate.

func onMessagesRead(receipt: MessageReceipt) {

print("onMessagesRead \(receipt.stringValue())")
}

func onMessagesDelivered(receipt: MessageReceipt) {

print("onMessagesDelivered \(receipt.stringValue())")
}

You will receive events in the form of MessageReceipt objects. The message receipt contains the below parameters:

ParameterInformation
messageIdThe Id of the message prior to which all the messages for that particular conversation have been marked as read.
senderUser object containing the details of the user who has marked the message as read.
receiverIdId of the receiver whose conversation has been marked as read.
receiverTypetype of the receiver (user/group)
receiptTypeType of the receipt (read/delivered)
deliveredAtThe timestamp of the time when the message was delivered. This will only be present if the receiptType is delivered.
readAtThe timestamp of the time when the message was read. This will only be present when the receiptType is read.

While fetching messages in bulk, if the message is a part of a user conversation, the message object will have two fields viz. deliveredAt and readAt which hold the timestamp for the time the message was delivered and read respectively. using these two variables, the delivery and read status for a user message can be obtained.

However, for a group message, the deliveredAt and readAt fields are never set and hence to get the status of the message you can use the below-described method.

Missed Receipts

You will receive message receipts when you load offline messages. For groups, we do not include the list the users that have received it. You need to call the getMessageReceipts() method for it.

Receipt History for a Single Message

In order to fetch the message receipts, you can use the getMessageReceipts() method.

var messageId = 10101;

CometChat.getMessageReceipts(messageId, onSuccess: { (receipt) in

print("getMessageReceipt \(receipt)")
}) { (error) in
print(error?.errorDescription)
}

You will receive a list of MessageReceipt objects in the onSuccess() method.