One-to-One and Group Chat Read Receipts
The read receipt feature allows senders to know whether their messages have been read by recipients.
- In one-to-one chats: When a recipient reads a message, they can actively send a read receipt to the sender, who receives the read notification by listening for read receipt messages.
- In group chats: After a message is sent to a group, the sender can actively initiate a read receipt request. Other group members who read the message can respond to the request. The sender learns which group members have read the message by listening for read receipt responses.
The SDK provides a ReadReceiptListener to handle event notifications related to read receipts in both one-to-one and group chats.
One-to-One Chat Read Receipts
For one-to-one chats, the read receipt feature requires the message recipient to pass the message read timestamp to the sender. The sender uses this timestamp to track reading progress. The IMLib SDK defines SentStatus to record whether a message in a one-to-one conversation has been read by the recipient. You can query the specific read status by checking the sentStatus property of messages sent by the local user.
Sending One-to-One Read Receipts
After reading a message, the recipient needs to call the sendReadReceiptMessage method with a timestamp to actively send a read receipt to the sender. The timestamp can be the send time of a specific message (message.getSentTime()) or the send time of the last message in the conversation (conversation.getSentTime()). A one-to-one read receipt is essentially a read notification message constructed and sent internally by the SDK.
Example Code
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "User ID of the receipt recipient";
long timestamp = message.getSentTime();// Send time of the received message
RongCoreClient.getInstance().sendReadReceiptMessage(conversationType, targetId, timestamp, new IRongCoreCallback.ISendMessageCallback() {
@Override
public void onAttached(Message message) {
}
@Override
public void onSuccess(Message message) {
}
@Override
public void onError(Message message, IRongCoreEnum.CoreErrorCode CoreErrorCode) {
}
});
Receiving One-to-One Read Receipts
The sender must use setReadReceiptListener to set up a read receipt listener to receive notifications when one-to-one read receipts arrive. When the IMLib SDK receives a one-to-one message read receipt, it extracts the timestamp and updates the SentStatus property of all messages in the local message database that were sent before this timestamp in the one-to-one conversation to READ. It also triggers the onReadReceiptReceived method of ReadReceiptListener to receive such notifications.
Example Code
RongCoreClient.setReadReceiptListener(new IRongCoreListener.ReadReceiptListener() {
@Override
public void onReadReceiptReceived(final Message message) {
// This is the callback method for one-to-one message receipts, where receipt message processing can be performed
Long timestamp = message.getContent().getLastMessageSendTime();
}
@Override
public void onMessageReceiptRequest(Conversation.ConversationType type, String targetId, String messageUId) {
// This is the callback method for group chat read receipt requests
}
@Override
public void onMessageReceiptResponse(Conversation.ConversationType type, String targetId, String messageUId, HashMap<String, Long> respondUserIdList) {
// This is the callback method for group chat read receipt responses
}
});
Applications can obtain the Message object from the onReadReceiptReceived method and extract the timestamp from the ReadReceiptMessage object in its content property. When displaying messages sent by the local user in the UI, messages sent before this timestamp can be shown as read by the recipient.
Group Chat Read Receipts
The group chat read receipt feature is based on the globally unique message ID (messageUId). By passing the messageUId in group conversations, the message sender can obtain the reading progress and list of users who have read a specific message. The SDK defines ReadReceiptInfo to record group chat read receipt data for a message. Applications can query the specific status by checking the ReadReceiptInfo property of messages sent by the local user.
The group chat read receipt workflow is as follows:
- Group member Alice sends a message to the group. The message reaches the IM server and is then delivered to the target group.
- Alice wants to know which group members have read the message. As the sender, she can initiate a group message read receipt request.
- Other group members listen for read receipt requests initiated by Alice.
- Group member Bob reads the message sent by Alice and responds to the read receipt request in the group.
- As the message sender, Alice can listen for read receipt responses to the message. After receiving Bob's response in the group, she can update the message read count.
Initiating Group Chat Read Receipt Requests
After sending a message to the group, the sender must actively initiate a read receipt request to obtain active responses from group members and track the message's read status. In a group conversation, only the message sender can initiate read receipt requests.
After sending the message, the sender uses the sendReadReceiptRequest method with the sent Message object to initiate a read receipt request for the message. The SDK internally constructs and sends a group chat read receipt request message.
Example Code
RongCoreClient.getInstance().sendReadReceiptRequest(message, new IRongCoreCallback.OperationCallback(){
@Override
public void onSuccess() {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
}
});
Receiving Group Chat Read Receipt Requests
All group members must use setReadReceiptListener to set up a read receipt listener to receive read receipt requests in the group. When any group member initiates a read receipt request, other members receive the request through the onMessageReceiptRequest callback of ReadReceiptListener.
@Override
public void onMessageReceiptRequest(Conversation.ConversationType type, String targetId, String messageUId) {
// Received a group chat read receipt request
}
The onMessageReceiptRequest method returns the message UID. You can use the getMessageByUid method to obtain the message object for responding to the read receipt request. To batch retrieve messages, use the getBatchLocalMessages method under ChannelClient.
Responding to Group Chat Read Receipt Requests
Message recipients can respond to read receipt requests based on their reading progress. If multiple requests are received at once, they can be batched, but only for read receipt requests within the same conversation.
Recipients can use the sendReadReceiptResponse method with the conversation type, group ID, and Message object list to initiate a read receipt response. Note that the conversation type must be ConversationType.GROUP.
Example Code
Conversation.ConversationType conversationType = ConversationType.GROUP;
String targetId = "Group ID";
List<Message> messageList = new ArrayList<>();
messageList.add(message1);
RongCoreClient.getInstance().sendReadReceiptResponse(conversationType, targetId, messageList, new IRongCoreCallback.OperationCallback() {
public void onSuccess() {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode coreErrorCode) {
}
});
Receiving Group Chat Read Receipt Responses
After a group member successfully responds, the request initiator receives the notification and response results through the onMessageReceiptResponse callback of ReadReceiptListener. Note that only the request initiator receives the response.
Example Code
@Override
public void onMessageReceiptResponse(Conversation.ConversationType type, String targetId, String messageUId, HashMap<String, Long> respondUserIdList) {
// Received a response to the group chat read receipt request
}
Applications can obtain the message UID and list of responding users from the onMessageReceiptResponse method. When displaying the message sent by the local user in the UI, the read count and specific list of users who have read it can be shown. The SDK stores the group message read receipt data in the ReadReceiptInfo property of the message.