Message receipts
After a user sends a message to another chat user or chat group, this user expects to know whether the message is delivered or read. The Chat SDK provides the message receipt feature, which enables you to send a receipt to the message sender once the message is delivered or read.
This page introduces how to use the Chat SDK to implement message receipt functionalities in one-to-one chats and chat groups.
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
- The message sender enables delivery receipts by setting
ChatOptions.setRequireDeliveryAck
astrue
. - After the recipient receives the message, the SDK automatically sends a delivery receipt to the sender.
- The sender receives the delivery receipt by listening for
onMessageDelivered
.
- The message sender enables delivery receipts by setting
-
Conversation and message read receipts
- The message sender enables read receipt by setting
ChatOptions.setRequireAck
astrue
. - After reading the message, the recipient calls
ackConversationRead
orackMessageRead
to send a conversation or message read receipt. - The sender receives the conversation or message receipt by listening for
onConversationRead
oronMessageRead
.
- The message sender enables read receipt by setting
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:
-
The message sender sets
setRequireDeliveryAck
inChatOptions
astrue
before sending the message: -
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.
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.
- When a user enters the conversation UI, check whether the conversation contains unread messages. If yes, call
ackConversationRead
to send a conversation read receipt.
- The message sender listens for message events and receives the conversation read receipt in
onConversationRead
.
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 receiveonConversationRead
. - When a user enters the conversation UI, check whether the conversation contains unread messages. If yes, call
-
Message read receipts
To implement the message read receipt, take the following steps:
- Send a conversation read receipt when the recipient enters the conversation.
- When a new message arrives, send the message read receipt and add proper handling logics for the different message types.
- The message sender listens for the message receipt:
Chat groups
In group chats, you can use message read receipts to notify the group owner or admins that the chat group message has been read. Ensure that each group member that has read the message should send a message read receipt.
Follow the steps to implement chat message read receipts.
-
For chat group messages, the group owner and admins can set to require the message read receipt when sending the message.
-
After the group member reads the chat group message, call
sendAckMessage
from the group member's client to send a message read receipt: -
The message sender listens for the message read receipt.
-
The message sender can get the detailed informaiton of the read receipt using
asyncFetchGroupReadAcks
.