Read receipt

Update time: 2024/03/07 11:13:59

If you want to know whether a recipient has read the sent message, use the read receipt.

The MessageService class provides methods used to send read receipts and corresponding listeners. When calling the method that sends a read receipt, the message passed in the method is the message that is shown as read.

Read receipt for messages in P2P chats

API call sequence diagram

The sequence diagrams in this document may display abnormally due to network problems. To fix the displayed problem, refresh the current page.

uml diagram

Prerequisites

Implementation

  1. The sender registers the onMessageReceipt event stream and listens for receiving read receipts NIMMessageReceipt.

    • NIMMessageReceiptrepresentation
    Parameter Type Description
    sessionId String Session ID, The ID of a chat object (accid)
    time int The time of the last read message in a session. All messages earlier than the last read messages are taken as read.
    • Example
    dartNimCore.instance.messageService.onMessageReceipt.listen((event) {
      print('Test_Observer onMessageReceipt ${event.toString()}');
    });
    
    
  2. (Optional) After sending a message, the sender can check whether the recipient has read the message using isRemoteRead.

  3. After the recipient receives the message and reads it, the recipient send a read receipt by calling the [sendMessageReceipt](https://doc.commsease.com/docs/interface/IM_Android/com/netease/nimlib/sdk /msg/MsgService.html#sendMessageReceipt-java.lang.String-com.netease.nimlib.sdk.msg.model.IMMessage-) method with the received message as argument.

    If this method is called in the session interface and the last message of the current session is passed in, the previous messages have been read by the current user.

    Sample code:

    dartNimCore.instance.messageService.onMessage.listen((event) {
      print('Test_Observer onMessage ${event.toString()}');
      for (NIMMessage m in event) {
        if (m.sessionType == NIMSessionType.p2p) {
          NimCore.instance.messageService.sendMessageReceipt(sessionId: m.fromAccount!, message: m);
        } else if (m.sessionType == NIMSessionType.team) {
          NimCore.instance.messageService.sendTeamMessageReceipt(m);
        }
      }
    });
    
    
  4. The sender registers the onMessageReceipt event stream and listens for receiving read receipts ( NIMMessageReceipt).

Read receipts for messages in group chats

This section describes how to implement read receipts for messages in a group chat.

To use read receipt for group messages, you must activate the service. Contact sales using WhatsApp on the CommsEase website for help.

API call sequence diagram

The sequence diagrams in this document may display abnormally due to network problems. To fix the displayed problem, refresh the current page.

uml diagram

Prerequisites

  • IM accounts are created.

  • You have contacted the business manager to activate the read receipt for group messages.

  • The recipient has registered the onMessage event stream that listens to message receiving.

Limits

To use the read receipt for group messages, the group must have members within 100 people.

Implementation

  1. When the sender and recipients initialize the SDK, set NIMSDKOptions.enableTeamMessageReadReceipt to true to enable the read receipt for group messages.

  2. The sender registers the observer for read receipt for group messages by calling onTeamMessageReceipt before login. The observer tracks the read receipt for group messages <List<NIMTeamMessageReceipt>>.

    Parameter Type Description
    messageId String Message ID
    ackCount int The number of users who have read the message
    unAckCount int The number of user who have not read the message
    • Example
    dartNimCore.instance.messageService.onTeamMessageReceipt.listen((event) {
      print('Test_Observer onTeamMessageReceipt ${event.toString()}');
    });
    
    
  3. The sender creates and sends a message, specifying whether the message requires a read receipt using NIMMessage.messageAck.

    Sample code:

    dart// Create a message.
          NIMResult<NIMMessage> result = await MessageBuilder.createTextMessage(sessionId: '123', sessionType: NIMSessionType.team, text: 'text');
          if (result.isSuccess) {
            var message = result.data!;
            message.messageAck = true;
            NimCore.instance.messageService.sendMessage(message: message);
          }
    
  4. Recipients evaluate whether the message requires a read receipt using the NIMMessage.messageAck attribute of the message.

  5. To send a read receipt, recipients evaluates whether a read receipt has been sent for the message using NIMMessage#hasSendAck.

  6. Recipients send a read receipt for the message by calling the sendTeamMessageReceipt method.

    • Example
    dartNimCore.instance.messageService.sendTeamMessageReceipt(message)
    
  7. The sender receives the read receipt returned by the Stream callback of onTeamMessageReceipt.

What’s next

If the sender gets the read receipt of a group message, the following methods are used to refresh the unread count of the message, query the list of accounts that have read or unread the message, or query the read or unread count of a message.

Refresh the read or unread count of multiple messages

Refresh the read or unread count of multiple messages by calling the refreshTeamMessageReceipt method. In most cases, refresh the read or unread count of multiple messages when loading messages. If you refresh the interface repeatedly, the SDK doesn't make requests. This method does not have an asynchronous callback. If the read or unread count changes, you must register the onTeamMessageReceipt event stream to listen to the changes and get notifications. If no changes occur, no notifications will be sent.

A maximum of 50 message can be passed for a call. In other words, the read/unread counts of up to 50 messages can be refreshed at a time.


dart// messages are the messages received
NimCore.instance.messageService.refreshTeamMessageReceipt(messages);

Query the list of accounts that have read or unread a group message

Query the list of accounts that have read or unread a group message by calling the fetchTeamMessageReceiptDetail method.

dartNimCore.instance.messageService.fetchTeamMessageReceiptDetail(message: message, accountList: ['123', '456']);

Query the list of accounts that have read or unread a group message from the local database

Get the accounts that have read or unread a group message from the local database by calling the queryTeamMessageReceiptDetail method.

The data of accounts that have read and unread the group fetched using this method may be outdated. To get the newest data, call the fetchTeamMessageReceiptDetail method.


dartNimCore.instance.messageService. queryTeamMessageReceiptDetail(message: message, accountList: ['123', '456']);

Get the number of accounts that have read or unread a message

Get the number of accounts that have read a message by calling the ackCount attribute of NIMMessage and the number of accounts that have unread a message by calling the unAckCount attribute.

API reference

API
Description
onMessageReceipt Register the event stream for read receipts in P2P chats, listening to the read receipts.
sendMessageReceipt Send a read receipt for a message in P2P chats.
onTeamMessageReceipt Register the event stream for read receipts of messages in group chats. If an observer is registered, the observer tracks the read receipts of messages in group chats.
sendTeamMessageReceipt Send a read receipt for a message in group chats.
refreshTeamMessageReceipt Refresh the read and unread counts of multiple messages.
fetchTeamMessageReceiptDetail Query the list of accounts that have read or unread a message.
queryTeamMessageReceiptDetail Get the specified accounts that have read or unread a group message from the local database.
Was this page helpful?
Yes
No
  • Read receipt for messages in P2P chats
  • API call sequence diagram
  • Prerequisites
  • Implementation
  • Read receipts for messages in group chats
  • API call sequence diagram
  • Prerequisites
  • Limits
  • Implementation
  • What’s next
  • Refresh the read or unread count of multiple messages
  • Query the list of accounts that have read or unread a group message
  • Query the list of accounts that have read or unread a group message from the local database
  • Get the number of accounts that have read or unread a message
  • API reference