Read Receipt for One-to-One and Group Chat
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, after the recipient has read the message, they can actively send a read receipt to the sender. The sender is notified of the read status by listening to the read receipt message.
- In a group chat scenario, after a message is sent to the group, the sender can actively initiate a read receipt request. Other group members can respond to the request after reading the message. The sender can then know which group members have read the message by listening to the read receipt response results.
The SDK provides a ReadReceiptListener for handling event notifications related to read receipts in one-to-one and group chats.
Read Receipt for One-to-One Chat
The read receipt feature for one-to-one chat is based on the timestamp of the message at the user's last read position in a one-to-one conversation. By passing this timestamp to the other party, the other party can track the reading progress of the sent messages. In a one-to-one conversation, the SentStatus property of the message sent by the local user records whether the message has been read by the other party.
Sending a Read Receipt for One-to-One Chat
After reading a message, the recipient needs to actively send a read receipt to the sender. When calling the sendReadReceiptMessage method, a timestamp needs to be passed in. You can pass in the send time of a specific message (message.getSentTime()
) or the send time of the last message in the conversation (conversation.getSentTime()
).
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "User ID of the 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) {
}
});
The read receipt for a one-to-one chat is actually a read notification message, which is constructed and sent internally by the SDK. The content
property of the Message
object returned in the callback contains a message content object of type ReadReceiptMessage.
Receiving a Read Receipt for One-to-One Chat
The sender needs to use setReadReceiptListener to set up a read receipt listener to receive notifications when a read receipt is received. After the SDK receives a read receipt for a one-to-one message, it reads the timestamp carried in the receipt and changes the SentStatus property of all messages in the local message database that are earlier than this timestamp to READ
. It also triggers the onReadReceiptReceived
method of the ReadReceiptListener.
RongCoreClient.setReadReceiptListener(new IRongCoreListener.ReadReceiptListener() {
@Override
public void onReadReceiptReceived(final Message message) {
// This is the callback method for one-to-one chat read receipts. You can handle the receipt message here.
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.
}
});
The application can obtain the Message
object from the onReadReceiptReceived
method and retrieve the timestamp carried in the message content object ReadReceiptMessage from its content
property. When displaying messages sent by the local user in the UI, messages earlier than this timestamp can be marked as read by the other party.
Read Receipt for Group Chat
The read receipt feature for group chat is based on the globally unique ID (UID) of the message. By passing the UID of the message in a group conversation, the sender can track the reading progress and the list of users who have read a specific message. In a group conversation, the ReadReceiptInfo property of the message sent by the local user records the read receipt data for that message.
The workflow for group chat read receipts 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 group members listen to the group message read receipt request initiated by Alice.
- Group member Bob reads the message sent by Alice and responds to Alice's read receipt request in the group.
- As the sender of the message, Alice can listen to and receive the read receipt response for the message. After receiving the response sent by Bob in the group, she can adjust the read count of the message.
Initiating a Group Chat Read Receipt Request
After the sender sends a message to the group, they must actively initiate a read receipt request to obtain active responses from group members in order to track the reading status of the message. In a group conversation, only the sender of the message can initiate a group chat read receipt request.
After the message is sent, the sender uses the sendReadReceiptRequest method, passing in the sent Message object, to initiate a read receipt request for the message. The SDK will internally construct and send a group chat read receipt request message.
RongCoreClient.getInstance().sendReadReceiptRequest(message, new IRongCoreCallback.OperationCallback(){
@Override
public void onSuccess() {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
}
});
Receiving a Group Chat Read Receipt Request
All group members need to use setReadReceiptListener to set up a read receipt listener to receive read receipt requests within the group. After any group member initiates a read receipt request, other group members will receive the read receipt request through the onMessageReceiptRequest
callback of the ReadReceiptListener.
@Override
public void onMessageReceiptRequest(Conversation.ConversationType type, String targetId, String messageUId) {
// Received a group chat read receipt request
}
The onMessageReceiptRequest
method returns the UID of the message. You can use the getMessageByUid method to retrieve the message object for subsequent responses to the read receipt request. If you need to retrieve messages in bulk, you can use the getBatchLocalMessages method under ChannelClient
.
Responding to a 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 respond to them in bulk, but you can only respond to read receipt requests from the same conversation at a time.
Use the sendReadReceiptResponse method, passing in the conversation type, group ID, and a list of Message objects, to initiate a read receipt response. Note that the conversation type must be ConversationType.GROUP
.
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 a Group Chat Read Receipt Response
After a user in the group responds successfully, the initiator of the request will receive a notification and the response result through the onMessageReceiptResponse
callback of the ReadReceiptListener. Note that only the initiator of the request can receive the response.
@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
}
The application can retrieve the UID of the message and the list of all responding users from the onMessageReceiptResponse
method. When displaying the message sent by the local user in the UI, you 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 for the group chat message in the ReadReceiptInfo property of the message.