메시지 읽음 확인
Feature Description
Read Receipt is used to notify the sender that "the recipient has read the sent message". When the recipient reads the message, a read message is reported, the backend system will generate a notification to inform the sender that the message has been viewed.
In instant messaging tools (WhatsApp, WeChat, etc.), when the recipient views the message, the sender will see a read receipt next to the message, such as a blue check mark or the word "read".
Note:
"Receipt" means "Reply Receipt", which represents a credential for confirming receipt. When you send a message and request a receipt, you are actually asking the other party "I want to confirm whether you have received and read my message". This confirmation is like a "receipt", proving that your message has been received.
Read receipts help ensure important messages has been viewed but can also cause psychological stress and privacy issues. Therefore, we support users to turn off the read receipt feature.
Note:
To use this feature, you need to purchase the Premium edition.
Read receipts for group messages are supported only by the Enhanced edition on v6.1.2155 or later.
Read receipts for one-to-one messages are supported only by the Enhanced edition on v6.2.2363 or later.
Effect
You can use this feature to implement the unread and read message effects shown in the figure below:
API Description
Setting Read Receipts for Group Messages
To enable read receipts for group messages, first configure the supported group type in the Console. Only the selected group types can use the read receipt feature. Configuration path: Applications > Your App > Chat > Configuration > Group Configuration > Group feature configuration > Read receipts for group messages.
You don't need to configure read receipts to be sent for one-to-one messages in the console, as they are supported by default.
Sending Message with Read Receipt (by the sender)
After creating a message, the sender specifies that the message requires a read receipt through the
needReadReceipt
field in V2TIMMessage
(Android / iOS and macOS / Windows) and then sends the message to the conversation.Sample code:
V2TIMMessage message = V2TIMManager.getMessageManager().createTextMessage("Group message read receipt");// Specify that the message requires a read receiptmessage.setNeedReadReceipt(true);// Send the messageV2TIMManager.getMessageManager().sendMessage(message, null, "groupA", V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {@Overridepublic void onProgress(int progress) {}@Overridepublic void onSuccess(V2TIMMessage message) {}@Overridepublic void onError(int code, String desc) {}});
/// Sample API callV2TIMMessage *message = [[V2TIMManager sharedInstance] createTextMessage:@"Group message read receipt"];// Specify that the message requires a read receiptmessage.needReadReceipt = YES;// Send the message[[V2TIMManager sharedInstance] sendMessage:message receiver:nil groupID:@"groupA" priority:V2TIM_PRIORITY_NORMAL onlineUserOnly:NO offlinePushInfo:nil progress:nil succ:nil fail:nil];
class SendCallback final : public V2TIMSendCallback {public:using SuccessCallback = std::function<void(const V2TIMMessage&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;using ProgressCallback = std::function<void(uint32_t)>;SendCallback() = default;~SendCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback,ProgressCallback progress_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);progress_callback_ = std::move(progress_callback);}void OnSuccess(const V2TIMMessage& message) override {if (success_callback_) {success_callback_(message);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}void OnProgress(uint32_t progress) override {if (progress_callback_) {progress_callback_(progress);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;ProgressCallback progress_callback_;};V2TIMMessage message = V2TIMManager::GetInstance()->GetMessageManager()->CreateTextMessage(u8"Group message read receipt");// Specify that the message requires a read receiptmessage.setNeedReadReceipt(true);auto callback = new SendCallback{};callback->SetCallback([=](const V2TIMMessage& message) { delete callback; },[=](int error_code, const V2TIMString& error_message) { delete callback; },[=](uint32_t progress) {});V2TIMManager::GetInstance()->GetMessageManager()->SendMessage(message, "groupA", {}, V2TIMMessagePriority::V2TIM_PRIORITY_DEFAULT, false, {}, callback);
Report Message Read (by the receiver)
After receiving the message, the receiver determines whether the message requires a read receipt based on the
needReadReceipt
field in V2TIMMessage
. If yes, after the user reads the message, the receiver calls the sendMessageReadReceipts
API (Android / iOS and macOS / Windows) to send a message read receipt.Sample code:
// Suppose the user has read the `message` messageif (!message.isSelf() && message.isNeedReadReceipt()) {List<V2TIMMessage> messageList = new ArrayList<>();messageList.add(message);V2TIMManager.getMessageManager().sendMessageReadReceipts(messageList, new V2TIMCallback() {@Overridepublic void onSuccess() {// Read receipt for the message sent successfully}@Overridepublic void onError(int code, String desc) {// Failed to send a read receipt for the message}});}
/// Sample API call/// Suppose the user has read the `msg` messageif (!msg.isSelf && msg.needReadReceipt) {[[V2TIMManager sharedInstance] sendMessageReadReceipts:@[msg] succ:^{// Read receipt for the message sent successfully} fail:^(int code, NSString *desc) {// Failed to send a read receipt for the message}];}
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new Callback;callback->SetCallback([=]() {// Read receipt for the message sent successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to send a read receipt for the messagedelete callback;});// Suppose the user has read the `message` messageif (!message.isSelf && message.needReadReceipt) {V2TIMMessageVector messageList;messageList.PushBack(message);V2TIMManager::GetInstance()->GetMessageManager()->SendMessageReadReceipts(messageList, callback);}
Listening for Read Receipt Notification (by the sender)
After the receiver sends a message read receipt, the sender will receive the read receipt notification in
onRecvMessageReadReceipts
of V2TIMAdvancedMsgListener
(Android / iOS and macOS / Windows) and update the UI based on the notification to display the message as, for example, "Read by two members".Sample code:
V2TIMAdvancedMsgListener advancedMsgListener = new V2TIMAdvancedMsgListener() {@Overridepublic void onRecvMessageReadReceipts(List<V2TIMMessageReceipt> receiptList) {for (V2TIMMessageReceipt receipt : receiptList) {// Message IDString msgID = receipt.getMsgID();// ID of the other party of the one-to-one messageString userID = receipt.getUserID();// Read status of the other party of the one-to-one messageboolean isPeerRead = receipt.isPeerRead();// Read timestamp of the other party of the one-to-one messagelong timestamp = receipt.getTimestamp();// Latest read count of the group messagelong readCount = receipt.getReadCount();// Latest unread count of the group messagelong unreadCount = receipt.getUnreadCount();}}};V2TIMManager.getMessageManager().addAdvancedMsgListener(advancedMsgListener);
/// Sample API call[[V2TIMManager sharedInstance] addAdvancedMsgListener:self];- (void)onRecvMessageReadReceipts:(NSArray<V2TIMMessageReceipt *> *)receiptList {for(V2TIMMessageReceipt *receipt in receiptList) {// Message IDNSString *msgID = receipt.msgID;// ID of the other party of the one-to-one messageNSString * userID = receipt.userID;// Read status of the other party of the one-to-one messageBOOL isPeerRead = receipt.isPeerRead;// Read timestamp of the other party of the one-to-one messagetime_t timestamp = receipt.timestamp;// Group IDNSString * groupID = receipt.groupID;// Latest read count of the group messageuint64_t readCount = receipt.readCount;// Latest unread count of the group messageuint64_t unreadCount = receipt.unreadCount;}}
class AdvancedMsgListener final : public V2TIMAdvancedMsgListener {public:void OnRecvMessageReadReceipts(const V2TIMMessageReceiptVector& receiptList) override {for (size_t i = 0; i < receiptList.Size(); ++i) {const V2TIMMessageReceipt& receipt = receiptList[i];// Message IDV2TIMString msgID = receipt.msgID;// ID of the other party of the one-to-one messageV2TIMString userID = receipt.userID;// Read status of the other party of the one-to-one messagebool isPeerRead = receipt.isPeerRead;// Read timestamp of the other party of the one-to-one messageint64_t timestamp = receipt.timestamp;// Group IDV2TIMString groupID = receipt.groupID;// Latest read count of the group messageint32_t readCount = receipt.readCount;// Latest unread count of the group messageint32_t unreadCount = receipt.unreadCount;}}// Other members ...};// Note that `advancedMsgListener` should not be released before the IM SDK is uninitialized,// otherwise the message callback cannot be called.AdvancedMsgListener advancedMsgListener;V2TIMManager::GetInstance()->GetMessageManager()->AddAdvancedMsgListener(&advancedMsgListener);
Pulling Read Receipt Detail (by the sender)
After entering the message list, the sender pulls historical messages first, and then calls the
getMessageReadReceipts
API (Android / iOS and macOS / Windows) to pull the message read receipt information.The
V2TIMMessageReceipt
field of the message read receipt is as described below:Attribute | Definition | Description |
msgID | Message ID | Unique message ID |
userID | ID of the receiver | If the message is a one-to-one message, this field indicates the ID of the receiver. |
isPeerRead | Whether the message is read by the receiver | If the message is a one-to-one message, this field indicates whether the message is read by the receiver. |
timestamp | The peer read time | If msgID is empty, this parameter indicates the time when the peer user mark the conversation as read. If msgID is not empty, this parameter indicates the time when the peer user send message read receipt (supported only in 8.1 and later versions). |
groupID | Group ID | If the message is a group message, this field indicates the group ID. |
readCount | Number of members who have read the group message | If the message is a group message, this field indicates the number of members who have read the message. |
unreadCount | Number of members who have not read the group message | If the message is a group message, this field indicates the number of members who have not read the message. |
Sample code:
/// Sample API call (taking a group message as an example)V2TIMManager.getMessageManager().getGroupHistoryMessageList("groupA", 20, null, new V2TIMValueCallback<List<V2TIMMessage>>() {@Overridepublic void onSuccess(final List<V2TIMMessage> v2TIMMessages) {List<V2TIMMessage> receiptMsgs = new ArrayList<>();// If the message sent by you requires a read receipt, the message read receipt information will need to be pulled.for (V2TIMMessage msg : v2TIMMessages) {if (msg.isSelf() && msg.isNeedReadReceipt()) {receiptMsgs.add(msg);}}V2TIMManager.getMessageManager().getMessageReadReceipts(receiptMsgs, new V2TIMValueCallback<List<V2TIMMessageReceipt>>() {@Overridepublic void onSuccess(List<V2TIMMessageReceipt> v2TIMMessageReceipts) {Map<String, V2TIMMessageReceipt> messageReceiptMap = new HashMap<>();for (V2TIMMessageReceipt receipt : v2TIMMessageReceipts) {messageReceiptMap.put(receipt.getMsgID(), receipt);}for (V2TIMMessage msg : v2TIMMessages) {V2TIMMessageReceipt receipt = messageReceiptMap.get(msg.getMsgID());if (receipt != null) {// ID of the other party of the one-to-one messageString userID = receipt.getUserID();// Read status of the other party of the one-to-one messageboolean isPeerRead = receipt.isPeerRead();// Read timestamp of the other party of the one-to-one messagelong timestamp = receipt.getTimestamp();// Group IDString groupID = receipt.getGroupID();// Message read count. If `readCount` is `0`, no one has read the message.long readCount = receipt.getReadCount();// Message unread count. If `unreadCount` is `0`, all members have read the message.long unreadCount = receipt.getUnreadCount();}}}@Overridepublic void onError(int code, String desc) {// Failed to pull the message read status}});}@Overridepublic void onError(int code, String desc) {// Failed to pull the message}});
/// Sample API call (taking a group message as an example)[[V2TIMManager sharedInstance] getGroupHistoryMessageList:@"groupA" count:20 lastMsg:nil succ:^(NSArray<V2TIMMessage *> *msgs) {NSMutableArray *receiptMsgs = [NSMutableArray array];// If the message sent by you requires a read receipt, the message read receipt information will need to be pulled.for (V2TIMMessage *msg in msgs) {if (msg.isSelf && msg.needReadReceipt) {[receiptMsgs addObject:msg];}}[[V2TIMManager sharedInstance] getMessageReadReceipts:receiptMsgs succ:^(NSArray<V2TIMMessageReceipt *> *receiptList) {NSMutableDictionary *param = [NSMutableDictionary dictionary];for (V2TIMMessageReceipt *receipt in receiptList) {[param setObject:receipt forKey:receipt.msgID];}for (V2TIMMessage *msg in msgs) {V2TIMMessageReceipt *receipt = param[msg.msgID];// ID of the other party of the one-to-one messageNSString * userID = receipt.userID;// Read status of the other party of the one-to-one messageBOOL isPeerRead = receipt.isPeerRead;// Read timestamp of the other party of the one-to-one messagetime_t timestamp = receipt.timestamp;// Group IDNSString * groupID = receipt.groupID;// Message read count. If `readCount` is `0`, no one has read the message.uint64_t readCount = receipt.readCount;// Message unread count. If `unreadCount` is `0`, all members have read the message.uint64_t unreadCount = receipt.unreadCount;}} fail:^(int code, NSString *desc) {// Failed to pull the message read status}];} fail:^(int code, NSString *desc) {// Failed to pull the message}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};// Sample API call (taking a group message as an example)V2TIMMessageListGetOption option;option.getType = V2TIMMessageGetType::V2TIM_GET_CLOUD_OLDER_MSG;option.count = 20;option.groupID = "groupA";auto callback = new ValueCallback<V2TIMMessageVector>{};callback->SetCallback([=](const V2TIMMessageVector& messageList) {V2TIMMessageVector receiptMsgs;for (size_t i = 0; i < messageList.Size(); ++i) {const V2TIMMessage& message = messageList[0];// If the message sent by you requires a read receipt,// the message read receipt information will need to be pulled.if (message.isSelf && message.needReadReceipt) {receiptMsgs.PushBack(message);}}auto receipt_callback = new ValueCallback<V2TIMMessageReceiptVector>{};receipt_callback->SetCallback([=](const V2TIMMessageReceiptVector& receiptList) {std::unordered_map<V2TIMString, V2TIMMessageReceipt> map;for (size_t i = 0; i < receiptList.Size(); ++i) {const V2TIMMessageReceipt& receipt = receiptList[i];map[receipt.msgID] = receipt;}for (size_t i = 0; i < messageList.Size(); ++i) {const V2TIMMessage& message = messageList[0];if (map.count(message.msgID)) {const V2TIMMessageReceipt& receipt = map[message.msgID];// ID of the other party of the one-to-one messageV2TIMString userID = receipt.userID;// Read status of the other party of the one-to-one messagebool isPeerRead = receipt.isPeerRead;// Read timestamp of the other party of the one-to-one messageint64_t timestamp = receipt.timestamp;// Group IDV2TIMString groupID = receipt.groupID;// Message read count. If `readCount` is `0`, no one has read the message.int32_t readCount = receipt.readCount;// Message unread count. If `unreadCount` is `0`, all members have read the message.int32_t unreadCount = receipt.unreadCount;}const V2TIMMessageReceipt& receipt = receiptList[i];}delete receipt_callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to pull the message read statusdelete receipt_callback;});V2TIMManager::GetInstance()->GetMessageManager()->GetMessageReadReceipts(messageList, receipt_callback);delete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to pull the messagedelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->GetHistoryMessageList(option, callback);
Pulling the Detail of Read Members (by the sender)
To view the list of members who have or have not read a group message, the sender can call the
getGroupMessageReadMemberList
API (Android / iOS and macOS / Windows) to pull the member list by page./// Sample API callV2TIMManager.getMessageManager().getGroupMessageReadMemberList(message, V2TIMMessage.V2TIM_GROUP_MESSAGE_READ_MEMBERS_FILTER_READ, 0, 100, new V2TIMValueCallback<V2TIMGroupMessageReadMemberList>() {@Overridepublic void onSuccess(V2TIMGroupMessageReadMemberList v2TIMGroupMessageReadMemberList) {// `members` is the list of members who have read the message pulled from the current page.List<V2TIMGroupMemberInfo> members = v2TIMGroupMessageReadMemberList.getMemberInfoList();// `nextSeq` indicates the cursor position for the next pull.long nextSeq = v2TIMGroupMessageReadMemberList.getNextSeq();// `isFinished` indicates whether the list of members who have read the message has been fully pulled.boolean isFinished = v2TIMGroupMessageReadMemberList.isFinished();// If the list of members who have read the message is not fully pulled, continue with the next pull (here is only the sample code. We recommend paged pull be triggered by a user click).if (!isFinished) {V2TIMManager.getMessageManager().getGroupMessageReadMemberList(message, V2TIMMessage.V2TIM_GROUP_MESSAGE_READ_MEMBERS_FILTER_READ, nextSeq, 100, new V2TIMValueCallback<V2TIMGroupMessageReadMemberList>() {@Overridepublic void onSuccess(V2TIMGroupMessageReadMemberList v2TIMGroupMessageReadMemberList) {// List of members who have read the message pulled successfully}@Overridepublic void onError(int code, String desc) {// Failed to pull the list of members who have read the message}});}}@Overridepublic void onError(int code, String desc) {// Failed to pull the list of members who have read the message}});
/// Sample API call[[V2TIMManager sharedInstance] getGroupMessageReadMemberList:message filter:V2TIM_GROUP_MESSAGE_READ_MEMBERS_FILTER_READ nextSeq:0 count:100 succ:^(NSMutableArray<V2TIMGroupMemberInfo *> *members, uint64_t nextSeq, BOOL isFinished) {// `members` is the list of members who have read the message pulled from the current page.// `nextSeq` indicates the cursor position for the next pull.// `isFinished` indicates whether the list of members who have read the message has been fully pulled.// If the list of members who have read the message is not fully pulled, continue with the next pull (here is only the sample code. We recommend paged pull be triggered by a user click).if (!isFinished) {[[V2TIMManager sharedInstance] getGroupMessageReadMemberList:message filter:V2TIM_GROUP_MESSAGE_READ_MEMBERS_FILTER_READ nextSeq:nextSeq count:100 succ:^(NSMutableArray<V2TIMGroupMemberInfo *> *members, uint64_t nextSeq, BOOL isFinished) {// List of members who have read the message pulled successfully} fail:^(int code, NSString *desc) {// Failed to pull the list of members who have read the message}];}} fail:^(int code, NSString *desc) {// Failed to pull the list of members who have read the message}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};auto callback = new ValueCallback<V2TIMGroupMessageReadMemberList>{};callback->SetCallback([=](const V2TIMGroupMessageReadMemberList& groupMessageReadMemberList) {// `members` is the list of members who have read the message pulled from the current page.const V2TIMGroupMemberInfoVector& members = groupMessageReadMemberList.members;// `nextSeq` indicates the cursor position for the next pull.uint64_t nextSeq = groupMessageReadMemberList.nextSeq;// `isFinished` indicates whether the list of members who have read the message has been fully pulled.bool isFinished = groupMessageReadMemberList.isFinished;// If the list of members who have read the message is not fully pulled,// continue with the next pull (here is only the sample code.// We recommend paged pull be triggered by a user click).if (!isFinished) {auto callback = new ValueCallback<V2TIMGroupMessageReadMemberList>{};callback->SetCallback([=](const V2TIMGroupMessageReadMemberList& groupMessageReadMemberList) {// List of members who have read the message pulled successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to pull the list of members who have read the messagedelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->GetGroupMessageReadMemberList(message, V2TIMGroupMessageReadMembersFilter::V2TIM_GROUP_MESSAGE_READ_MEMBERS_FILTER_READ,nextSeq, 100, callback);}delete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to pull the list of members who have read the messagedelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->GetGroupMessageReadMemberList(message, V2TIMGroupMessageReadMembersFilter::V2TIM_GROUP_MESSAGE_READ_MEMBERS_FILTER_READ, 0, 100,callback);