One-to-One and Group Chat Read Receipts
The read receipt feature allows the sender to know whether the message has been read by the recipient.
- In a one-to-one chat scenario, when the recipient reads the message, they can actively send a read receipt to the sender. The sender can then receive the read notification by listening for the read receipt message.
- In a group chat scenario, when a message is sent to a group, the sender can actively initiate a read receipt request after the message is sent. Other members in the group can respond to the request after reading the message. The sender can then learn which group members have read the message by listening for the read receipt response.
The SDK provides different event handling mechanisms for read receipts in one-to-one and group chats:
- The one-to-one chat read receipt uses the observer pattern, where the application needs to receive the RCLibDispatchReadReceiptNotification notification distributed by the SDK.
- The group chat read receipt uses the delegate pattern, employing the proxy methods in the RCIMClientReceiveMessageDelegate protocol for receiving messages.
One-to-One Chat Read Receipt
The one-to-one chat read receipt feature is based on the timestamp of the message last read by the user in a one-to-one conversation. By passing this timestamp to the other party, the other party can obtain the reading progress of the sent messages. The SDK defines RCSentStatus to record whether a message in a one-to-one conversation has been read by the other party. The application can query the specific status by accessing the sentStatus
property of the messages sent by the local end.
Sending One-to-One Chat Read Receipt
After reading a message, the message recipient needs to actively send a read receipt to the sender. When calling the sendReadReceiptMessage:targetId:time:success:error: method, a timestamp needs to be passed in. You can pass in the send time of the specified message (message.sentTime
) or the send time of the last message in the conversation (conversation.sentTime
).
[[RCCoreClient sharedCoreClient]
sendReadReceiptMessage:ConversationType_PRIVATE
targetId:@"targetId"
time:sentTime
success:nil
error:nil];
The one-to-one chat read receipt is actually a read notification message, which is constructed and sent internally by the SDK.
Receiving One-to-One Chat Read Receipt
After the SDK receives a read receipt for a one-to-one chat message, it reads the timestamp carried in it and changes the sentStatus
property of all messages in the local message database that are earlier than this timestamp to SentStatus_READ
. At the same time, the IMLib SDK will distribute a RCLibDispatchReadReceiptNotification notification. The application needs to register as an observer of the read receipt notification to receive such notifications.
This code registers the current object (self
) as an observer of the RCLibDispatchReadReceiptNotification
notification. When this notification is received, the didReceiveReadReceiptNotification:
method will be called.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didReceiveReadReceiptNotification:)
name:RCLibDispatchReadReceiptNotification
object:nil];
The didReceiveReadReceiptNotification:
method is called when a read receipt notification is received. This method accepts an NSNotification
object as a parameter. This object can be used to obtain the information of the read receipt notification.
- (void)didReceiveReadReceiptNotification:(NSNotification *)notification {
RCConversationType conversationType = (RCConversationType)[notification.userInfo[@"cType"] integerValue];
long long readTime = [notification.userInfo[@"messageTime"] longLongValue];
NSString *targetId = notification.userInfo[@"tId"];
NSString *senderUserId = notification.userInfo[@"fId"];
}
This code retrieves the following information from the notification's userInfo
dictionary:
- Conversation type (
conversationType
) - Read time (
readTime
) - Target ID (
targetId
) - Sender ID (
senderUserId
)
The readTime
above is the timestamp carried in the one-to-one chat read receipt message. At this point, the send status property (sentStatus
) of all messages in the message database with a send time less than or equal to readTime
has been changed to read by the other party (SentStatus_READ
). When the application displays the messages sent by the local end in the UI, it can show all messages earlier than this timestamp as read by the other party.
Group Chat Read Receipt
The group chat read receipt feature is based on the globally unique ID (UID) of the message. By passing the message's UID in a group chat conversation, the message sender can obtain the reading progress and the list of users who have read a specific message. The SDK defines RCReadReceiptInfo to record the read receipt data of a message in a group chat conversation. The application can query the specific status by accessing the readReceiptInfo
property of the messages sent by the local end.
The workflow of the group chat read receipt is as follows:
- Group member Alice sends a message to the group. The message will reach the IM server and be sent to the target group by the IM server.
- Alice wants to know the list of group members who have read the message. As the sender of the message, she can initiate a group message read receipt request.
- Other members in the group listen to the group message read receipt request initiated by Alice.
- Group member Bob reads the group message sent by Alice and then responds to Alice's read receipt request in the group.
- As the message sender, Alice can listen for the read receipt response of the message. After receiving the response sent by Bob in the group, she can adjust the read count of the message.
Initiating Group Chat Read Receipt Request
After sending a message to the group, the sender must actively initiate a read receipt request to obtain the active response of the group members in order to get the reading status of the message. In a group conversation, only the message sender can initiate a group chat read receipt request.
After the message is sent, the message sender uses the sendReadReceiptRequest:success:error: method, passing in the sent RCMessage object, to initiate a read receipt request for this message. The SDK will internally construct and send a group chat read receipt request message.
[[RCCoreClient sharedCoreClient] sendReadReceiptRequest:message
success:^{
}error:^(RCErrorCode nErrorCode) {
}];
Receiving Group Chat Read Receipt Request
The SDK defines the relevant proxy methods for group chat read receipts in the RCIMClientReceiveMessageDelegate protocol for receiving messages.
The application can set multiple message receiving delegates using the addReceiveMessageDelegate
method.
[[RCCoreClient sharedCoreClient] addReceiveMessageDelegate:self];
After any group member initiates a read receipt request, other group members will receive the read receipt request through the onMessageReceiptRequest
proxy method of RCIMClientReceiveMessageDelegate.
/*!
Request message read receipt (receive a request that requires a read receipt to be sent. After receiving this request, if the message corresponding to the messageUId has been displayed in the conversation page or the message obtained by calling getHistoryMessages includes this messageUId, you need to call the sendMessageReadReceiptResponse interface to send the message read receipt)
@param messageUId The message ID of the read receipt request
@param conversationType conversationType
@param targetId targetId
*/
- (void)onMessageReceiptRequest:(RCConversationType)conversationType
targetId:(NSString *)targetId
messageUId:(NSString *)messageUId;
The onMessageReceiptRequest
method returns the message's UID. You can use the getMessageByUId:completion: method to obtain the message object for subsequent responses to the read receipt request. If you need to batch obtain messages, you can use the getBatchLocalMessages:targetId:channelId:messageUIDs:success:error: method under RCChannelClient
.
Responding to Group Chat Read Receipt Request
The application can respond to the read receipt request based on the user's reading progress. If multiple requests are received at once, you can batch respond to the read receipt requests, but you can only batch respond to read receipt requests in the same conversation at a time.
Use the sendReadReceiptResponse:targetId:messageList:success:error: method, passing in the conversation type, group ID, and RCMessage object list, to initiate a read receipt response. Note that the conversation type must be ConversationType_GROUP
.
[[RCCoreClient sharedCoreClient] sendReadReceiptResponse:ConversationType_GROUP
targetId:@"targetId"
messageList:msgList
success:^{
}
error:^(RCErrorCode nErrorCode){
}];
Receiving Group Chat Read Receipt Response
After a user in the group responds successfully, the requester will receive the notification and response result through the onMessageReceiptResponse
method of RCIMClientReceiveMessageDelegate. Note that only the requester can receive the response.
/*!
Message read receipt response (receive a read receipt response, you can update the read count of the message according to the messageUId)
@param messageUId The message ID of the read receipt request
@param conversationType RCConversationType
@param targetId targetId
@param userIdList List of user IDs who have read the message, dictionary type key:userId,value:readTime, for example: {"uid100001":1683708891523}
*/
- (void)onMessageReceiptResponse:(RCConversationType)conversationType
targetId:(NSString *)targetId
messageUId:(NSString *)messageUId
readerList:(NSMutableDictionary *)userIdList;
The application can obtain the message's UID and the list of all users who have responded from the onMessageReceiptResponse
method. When displaying the message sent by the local end in the UI, it can show the number of readers and the specific list of users who have read the message. The SDK will store the read receipt data of the group chat message in the readReceiptInf
property.