logo
On this page

Read receipts


Overview

Message reading receipt helps users know whether other users have read the messages they sent in a conversation. This feature applies to enterprise office businesses and other scenarios in which the message reading status needs to be known in real time.

This document describes how to use APIs of the In-app Chat SDK to send messages that require a reading receipt, query the receipt status of messages, and set messages as read.

Warning

The In-app Chat SDK supports reading receipts for one-to-one messages and group messages (only common messages and rich media messages) and does not support reading receipts for in-room messages.

Implementation process

The sender sends a message through the In-app Chat SDK and sets the hasReceipt field of ZIMMessageSendConfig to identify whether a reading receipt is required for the message. Based on the receiptStatus field, the receiver determines whether a reading receipt is required for the message or whether the message is read or unread to render different UI effects. The message receiver can use different reading methods based on the scenario.

Send a message that requires a reading receipt

When Client A wants to send a message that requires a reading receipt to Client B:

  1. Client A and Client B log in to the In-app Chat service.
  2. Client A calls the sendMessage or sendMediaMessage API to send a message (common message or rich media message in one-to-one or group chats) to Client B and sets the hasReceipt field of ZIMMessageSendConfig to true.
  3. By listening for related callback (onPeerMessageReceived or onGroupMessageReceived), Client B receives a message whose receiptStatus is set to PROCESSING.

Set the reading receipt status as read

In this operation, set a message as read and set a conversation as read are both supported.

Set a message as read

The receiver can set a message that requires a reading receipt from the sender as read. Then, the sender will receive a message read notification.

Warning
  • A single message or a batch of messages are supported. The sender and receiver must be in the same conversation. Cross-conversation operations are not supported.

  • To perform operations on the historical messages of the conversation, you need to get the historical messages and determine the receipt status of the historical messages. For details, see Get message history.

  1. Through related callback (onPeerMessageReceived or onGroupMessageReceived), Client B receives a message that requires a reading receipt from Client A.
  2. Based on the receiptStatus field of the callback, Client B determines the receipt status of the message. If this field is set to PROCESSING, the message is unread. Developers can call the sendMessageReceiptsRead API to set the message as read based on the service logic.
  3. Client B determines whether the setting is successful based on ZIMMessageReceiptsReadSentCallback.
  4. Based on onMessageReceiptChanged of ZIMEventHandler, Client A receives a callback notification, indicating that the message is set as read. Developers can implement the service logic of setting the message as read on Client A based on this callback.

Set a conversation as read

The receiver can set all messages received from the sender in a specified conversation as read.

Warning
  • The In-app Chat SDK supports this feature only in one-to-one chats.

  • This feature takes effect only on messages received before setting the feature.

  • It is recommended that this feature be used when a user switches from the conversation list page to a conversation. It is not recommended that this feature be used together with the sendMessageReceiptsRead API on a message chat page.

  • To perform operations on the historical messages of the conversation, you need to get the historical messages and determine the receipt status of the historical messages. For details, see Get message history.

  1. Based on the receiptStatus field of the onPeerMessageReceived callback, Client B determines the receipt status of the message. If this field is set to PROCESSING, the message is unread. Developers can call the sendConversationMessageReceiptRead API to set all messages sent by Client A in the conversation as read based on the service logic.
  2. Client B determines whether the setting is successful based on ZIMConversationMessageReceiptReadSentCallback .
  3. Based on onConversationMessageReceiptChanged of ZIMEventHandler , Client A receives a callback notification, indicating that all messages in the conversation are set as read. Developers can implement the logic of setting all messages sent from the sender in the conversation as read based on this callback. Developers can implement the service logic of knowing all sent messages in a conversation are set as read by Client B on Client A based on this callback.

More features

Batch query the message receipt status, number of users who have read the message, and number of users who have not read the message

To query the message receipt status, the number of users who have read the message, and the number of users who have not read the message of a message or a batch of messages, call the queryMessageReceiptsInfo API. Call ZIMMessageReceiptsInfoQueriedCallback to obtain related information.

Warning
  • If messages sent by other users are queried, the number of users who have read the message and the number of users who have not read the message are 0.

  • To perform operations on the historical messages of the conversation, you need to get the historical messages and determine the receipt status of the historical messages. For details, see Get message history.

Query the list of members who have or have not read a group message

The In-app Chat SDK supports querying the list of members who have or have not read a group message.

Query the list of members who have read a group message

To query the list of members who have read a group message, call the queryGroupMessageReceiptReadMemberList API.

Warning

To perform operations on the historical messages of the conversation, you need to get the historical messages and determine the receipt status of the historical messages. For details, see Get message history.

Query the list of members who have not read a group message

To query the list of members who have not read a group message, call the queryGroupMessageReceiptUnreadMemberList API.

Warning
  • If the SDK version is older than 2.16.0, when the number of group members is greater than 100, this API will not return the list of members who have not read a group message. To use this feature, contact ZEGOCLOUD technical support.

  • To perform operations on the historical messages of the conversation, you need to get the historical messages and determine the receipt status of the historical messages. For details, see Get message history.

Sample code

Untitled
// User A sends a message that requires a reading receipt. A text one-to-one message is used as an example.

zim::ZIMMessageSendConfig sendConfig;
zim::ZIMPushConfig pushConfig;

pushConfig.content = "win_push_content";
pushConfig.payload = "win_push_extended_data";
pushConfig.title = "win_push_title";

sendConfig.priority = zim::ZIM_MESSAGE_PRIORITY_MEDIUM;
sendConfig.pushConfig = &pushConfig;
sendConfig.hasReceipt = true; // Set that the messages require a reading receipt.

auto smessage = std::make_shared<zim::ZIMTextMessage>("test message");

auto notification = std::make_shared<zim::ZIMMessageSendNotification>(
    [=](const std::shared_ptr<zim::ZIMMessage> &message) { 
        
       //  Notification for message being stored in the database
    });

zim_->sendMessage(
    std::static_pointer_cast<zim::ZIMMessage>(smessage), userID,
    zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER, sendConfig, notification,
    [=](const std::shared_ptr<zim::ZIMMessage> &cb_message, const zim::ZIMError &errorInfo) {
        if (errorInfo.code == zim::ZIMErrorCode::ZIM_ERROR_CODE_SUCCESS) {
            // This indicates that the message has been successfully sent. The receiptStatus of the message will be PROCESSING, and the business layer can implement logic to display the unread receipt.
        }
    });


// User B receives the message that requires a reading receipt and sets the message as read by calling any of the following APIs.

// Set a message as read

std::vector<std::shared_ptr<zim::ZIMMessage>> messages;
messages.emplace_back(message);

zim_->sendMessageReceiptsRead(
    messages, conversationID, zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER,
    [=](const std::string &conversationID, zim::ZIMConversationType conversationType,
        const std::vector<long long> &errorMessageIDs,
        const zim::ZIMError &errorInfo) { 
        // Callback for setting a message as read.
    });

// Set a conversation as read

zim_->sendConversationMessageReceiptRead(
    conversationID, zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER,
    [=](const std::string &conversationID, zim::ZIMConversationType conversationType,
        const zim::ZIMError &errorInfo) { 
            // Callback for conversation read receipt. Developers can use this callback to set all messages sent by the other party in this conversation as read.
    });

// (Optional) Batch query the message receipt status, number of users who have read the message, and number of users who have not read the message.

std::vector<std::shared_ptr<zim::ZIMMessage>> messages;
messages.emplace_back(message);
zim_->queryMessageReceiptsInfo(
    messages, conversationID, zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER,
    [=](const std::vector<zim::ZIMMessageReceiptInfo> &infos,
        std::vector<long long> errorMessageIDs, const zim::ZIMError &errorInfo) {});


// (Optional) Query the list of members who have or have not read a group message.

// Read user list
zim::ZIMGroupMessageReceiptMemberQueryConfig readMemberQueryConfig;
readMemberQueryConfig.count = 10;    // Number of users to query.
readMemberQueryConfig.nextFlag = 0;    // Query flag, fill in 0 initially, and fill in the flag returned from the callback later.

zim_->queryGroupMessageReceiptReadMemberList(
    message, "group_id", readMemberQueryConfig,
    [=](const std::string &groupID, const std::vector<zim::ZIMGroupMemberInfo> &userList,
        unsigned int nextFlag, const zim::ZIMError &errorInfo) {
                if (errorInfo.code == zim::ZIMErrorCode::ZIM_ERROR_CODE_SUCCESS) {
                   // Query the corresponding member list
                }
        });

// Unread user list
zim::ZIMGroupMessageReceiptMemberQueryConfig unreadMemberQueryConfig;
unreadMemberQueryConfig.count = 10;    // Number of users to query.
unreadMemberQueryConfig.nextFlag = 0;    // Query flag, fill in 0 initially, and fill in the flag returned from the callback later.

zim_->queryGroupMessageReceiptUnreadMemberList(
    message, "group_id", unreadMemberQueryConfig,
    [=](const std::string &groupID, const std::vector<zim::ZIMGroupMemberInfo> &userList,
        unsigned int nextFlag, const zim::ZIMError &errorInfo) {
                if (errorInfo.code == zim::ZIMErrorCode::ZIM_ERROR_CODE_SUCCESS) {
                   // Query the corresponding member list
                }
        });
1
Copied!

Previous

Recall messages

Next

Set message extension field