Skip to main content

You are viewing Agora Docs forBetaproducts and features. Switch to Docs

Android
iOS
Web
Windows
Unity
Flutter
React Native

Message receipts

The Chat SDK provides the message receipt feature that allows the user, after sending a message, to know whether the message is delivered or read. Also, the Chat SDK supports the conversation read receipt that allows the message sender to know whether the conversation is read.

  • Message delivery receipt: Available only to one-to-one chats.
  • Message read receipt: Available to both one-to-one chats and group chats.
  • Conversation read receipt: Available only to one-to-one chats.

Understand the tech

The Chat SDK uses ChatManager to provide message receipt. Followings are the core methods:

  • ChatOptions.setRequireAck: Enables message read receipt.
  • ChatOptions.setRequireDeliveryAck: Enables message delivery receipt.
  • ackConversationRead: Sends a conversation read receipt.
  • ackMessageRead: Sends a message read receipt.
  • ackGroupMessageRead: Sends a message read receipt for group chat.

The logic for implementing these receipts are as follows:

  • Message delivery receipts

    1. The message sender enables delivery receipts by setting ChatOptions.setRequireDeliveryAck as true.
    2. After the recipient receives the message, the SDK automatically sends a delivery receipt to the sender.
    3. The sender receives the delivery receipt by listening for onMessageDelivered.
  • Conversation and message read receipts

    1. The message sender enables read receipt by setting ChatOptions.setRequireAck as true.
    2. After reading the message, the recipient calls ackConversationRead or ackMessageRead to send a conversation or message read receipt.
    3. The sender receives the conversation or message receipt by listening for onConversationRead or onMessageRead.

Prerequisites

Before proceeding, ensure that you meet the following requirements:

  • You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see Chat SDK quickstart.
  • You understand the API call frequency limits as described in Limitations.
  • Message read receipts for chat groups are not enabled by default. To use this feature, contact support@agora.io.

Implementation

This section introduces how to implement message delivery and read receipts in your chat app.

Message delivery receipts

To send a message delivery receipt, take the following steps:

  1. The message sender sets setRequireDeliveryAck in ChatOptions as true before sending the message:


    _4
    ChatOptions chatOptions = new ChatOptions();
    _4
    chatOptions.setRequireDeliveryAck(true);
    _4
    ...
    _4
    ChatClient.getInstance().init(mContext, chatOptions);

  2. Once the recipient receives the message, the SDK triggers onMessageDelivered on the message sender's client, notifying the message sender that the message has been delivered to the recipient.


    _15
    // Add a message listener to listen for the receipt message.
    _15
    MessageListener msgListener = new MessageListener() {
    _15
    // Occurs when the message is received.
    _15
    @Override
    _15
    public void onMessageReceived(List<ChatMessage> messages) {
    _15
    }
    _15
    // Occurs when the message delivery receipt is received
    _15
    @Override
    _15
    public void onMessageDelivered(List<ChatMessage> message) {
    _15
    }
    _15
    };
    _15
    // Register a message listener.
    _15
    ChatClient.getInstance().chatManager().addMessageListener(msgListener);
    _15
    // Remove the message listener when it is not used.
    _15
    ChatClient.getInstance().chatManager().removeMessageListener(msgListener);

Conversation and message read receipts

In both one-to-one chats and group chats, you can use message read receipts to notify the message sender that the message has been read. To minimize the method call for message read receipts, the SDK also supports conversation read receipts in one-to-one chats.

One-to-one chats

In one-to-one chats, the SDK supports sending both the conversation read receipts and message read receipts. Agora recommends using conversation read receipts if the new message arrives when the message recipient has not entered the conversation UI.

  • Conversation read receipts

    Follow the steps to implement conversation read receipts in one-to-one chats.

    1. When a user enters the conversation UI, check whether the conversation contains unread messages. If yes, call ackConversationRead to send a conversation read receipt.

    _7
    // The message receiver calls ackConversationRead to send the conversation read receipt.
    _7
    // This is an asynchronous method.
    _7
    try {
    _7
    ChatClient.getInstance().chatManager().ackConversationRead(conversationId);
    _7
    } catch (ChatException e) {
    _7
    e.printStackTrace();
    _7
    }

    1. The message sender listens for message events and receives the conversation read receipt in onConversationRead.

    _9
    // The message sender calls addConversationListener to listen for conversation events.
    _9
    ChatClient.getInstance().chatManager().addConversationListener(new ConversationListener() {
    _9
    ...
    _9
    @Override
    _9
    // Occurs when the all the messages in the conversation is read.
    _9
    public void onConversationRead(String from, String to) {
    _9
    // Add follow-up logics such as poping up a notification.
    _9
    }
    _9
    });

    In scenarios where a user is logged in multiple devices, if the user sends a conversation read receipt from one device, the server sets the count of unread messages in the conversation as 0, and all the other devices receive onConversationRead.
  • Message read receipts

    To implement the message read receipt, take the following steps:

    1. Send a conversation read receipt when the recipient enters the conversation.

    _6
    // The message receiver calls ackConversationRead to send the conversation read receipt.
    _6
    try {
    _6
    ChatClient.getInstance().chatManager().ackConversationRead(conversationId);
    _6
    }catch (ChatException e) {
    _6
    e.printStackTrace();
    _6
    }

    1. When a new message arrives, send the message read receipt and add proper handling logics for the different message types.

    _31
    ChatClient.getInstance().chatManager().addMessageListener(new MessageListener() {
    _31
    ......
    _31
    _31
    @Override
    _31
    // Occurs when the specified message is received.
    _31
    public void onMessageReceived(List<ChatMessage> messages) {
    _31
    ......
    _31
    // Send the message read receipt.
    _31
    sendReadAck(message);
    _31
    ......
    _31
    }
    _31
    ......
    _31
    });
    _31
    // Send the message read receipt.
    _31
    public void sendReadAck(ChatMessage message) {
    _31
    // For messages in one-to-one chat
    _31
    if(message.direct() == ChatMessage.Direct.RECEIVE
    _31
    undefined message.getChatType() == ChatMessage.ChatType.Chat) {
    _31
    ChatMessage.Type type = message.getType();
    _31
    // For voice, video, and file messages, you need to send the receipt after clicking the files.
    _31
    if(type == ChatMessage.Type.VIDEO || type == ChatMessage.Type.VOICE || type == ChatMessage.Type.FILE) {
    _31
    return;
    _31
    }
    _31
    try {
    _31
    // Call ackMessageRead to send the message read receipt.
    _31
    ChatClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
    _31
    } catch (ChatException e) {
    _31
    e.printStackTrace();
    _31
    }
    _31
    }
    _31
    }

    1. The message sender listens for the message receipt:

    _10
    // The message sender calls addMessageListener to listen for message events.
    _10
    ChatClient.getInstance().chatManager().addMessageListener(new MessageListener() {
    _10
    ......
    _10
    @Override
    _10
    // Occurs when the specified message is read.
    _10
    public void onMessageRead(List<ChatMessage> messages) {
    _10
    // Add follow-up logics such as poping up a notification.
    _10
    }
    _10
    ......
    _10
    });

Chat groups

For a group chat, group members can determine whether to require message read receipts when sending a message. If yes, after a group member reads the message, the SDK sends a read receipt. In a group chat, the number of message read receipts that are sent for the message refers to the number of group members that have read this message.

The following table shows the restrictions of this feature:

Feature RestrictionDefaultDescription
PermissionGroup owner and administratorsBy default, only the group owner and administrators can request read receipts when sending a message.
You can contact support@agora.io to grant the permission to regular group members.
Number of days before read receipts cannot be returned after the message is sent3 daysThe server no longer records the group members that read the message three days after it is sent, nor sends the read receipts.
Chat group size500 membersThis feature is available only to groups with up to 500 members. In other words, each message in a group can have up to 500 read receipts. If the upper limit is exceeded, the latest read receipt record will overwrite the earliest one.
Maximum number of group messages that can have read receipts per day500A group can have up to 500 messages each day for which read receipts can be returned.

Follow the steps to implement read receipts for a chat group message:

  1. When sending a message, a group member can set whether to require a message read receipt.


    _3
    // Set setIsNeedGroupAck as true when sending the group message
    _3
    ChatMessage message = ChatMessage.createTextSendMessage(content, to);
    _3
    message.setIsNeedGroupAck(true);

  2. After the group member reads the chat group message, call ackGroupMessageRead from the group member's client to send a message read receipt.


    _27
    // Send the group message read receipt.
    _27
    public void sendAckMessage(ChatMessage message) {
    _27
    if (!validateMessage(message)) {
    _27
    return;
    _27
    }
    _27
    _27
    if (message.isAcked()) {
    _27
    return;
    _27
    }
    _27
    _27
    // May a user login from multiple devices, so do not need to send the ack msg.
    _27
    if (ChatClient.getInstance().getCurrentUser().equalsIgnoreCase(message.getFrom())) {
    _27
    return;
    _27
    }
    _27
    _27
    try {
    _27
    if (message.isNeedGroupAck() && !message.isUnread()) {
    _27
    String to = message.conversationId(); // do not use getFrom() here
    _27
    String msgId = message.getMsgId();
    _27
    ChatClient.getInstance().chatManager().ackGroupMessageRead(to, msgId, ((TextMessageBody)message.getBody()).getMessage());
    _27
    message.setUnread(false);
    _27
    EMLog.i(TAG, "Send the group ack cmd-type message.");
    _27
    }
    _27
    } catch (Exception e) {
    _27
    EMLog.d(TAG, e.getMessage());
    _27
    }
    _27
    }

  3. The message sender listens for the message read receipt.


    _4
    // Occurs when the group message is read.
    _4
    void onGroupMessageRead(List<GroupReadAck> groupReadAcks) {
    _4
    // Add follow-up notifications
    _4
    }

  4. The message sender can get the detailed information of the read receipt using asyncFetchGroupReadAcks.


    _14
    // msgId: The message ID.
    _14
    // pageSize: The page size. The value range is [1,50].
    _14
    // startAckId: The starting receipt ID for query. Set it as null for the first call of the method and the SDK retrieves from the latest receipt.
    _14
    * @return The message receipt list and a cursor.
    _14
    */
    _14
    ChatClient.getInstance().chatManager().asyncFetchGroupReadAcks(msgId, pageSize, startAckId, new ValueCallBack<CursorResult<GroupReadAck>>() {
    _14
    @Override
    _14
    public void onSuccess(CursorResult<GroupReadAck> value) {// Succeeded in getting the details of the read receipt.
    _14
    }
    _14
    @Override
    _14
    public void onError(int error, String errorMsg) {
    _14
    // Failed to get the details of the read receipt.
    _14
    }
    _14
    });

vundefined