Android
Delivery Receipt

Delivery Receipt

Delivery receipt is a feature that indicates whether a message has successfully been delivered to all the intended recipients by Sendbird server. With the implementation of delivery receipt, Sendbird not only provides the timestamp of when each user has last read the messages in each channel, but also the timestamp of when each user has the message last delivered to each user in each channel.

DeliveryReceipt

Note: Delivery receipt is only applicable to group channels.


Benefits

Delivery receipt provides the following benefits.

Highly in demand feature

Most popular messaging apps, such as WhatsApp and Facebook Messenger, provide this feature. Users who have experienced them expect to see if the message they sent has been successfully delivered when using a new chat service. Delivery receipt is a feature in high demand that today’s users are accustomed to using.

Improved user experience

Previously, a sender had no way of knowing whether their message was unread because the server is in the process of delivering or failed to deliver the message due to the unreliable internet connection, or simply because the recipients haven’t yet read the message. This feature enables users to become better-informed, thus improving the Sendbird user experience.


How it works

Delivery receipt works in a similar way to read receipt. The server stores the timestamp of the message last delivered as delivered_ts. The timestamp is recorded per user, per channel.

SDK methods

Each SDK provides methods to make the Chat API's mark all messages as delivered action calls. If required, each SDK also makes necessary changes to handle the new response key, delivery_receipt, for APIs that return a group channel resource.

Delivery event

When a user sends a message to a group channel by calling the SDK’s sendUserMessage, it is considered a delivery event for Sendbird server.


Mark messages as delivered

To mark messages as delivered when a group channel member receives a push notification for the message from FCM, either the SendBird.markAsDelivered() static method or the groupChannel.markAsDelivered() instance method should be called.

Using a static method
Light Color Skin
Copy
public class FirebaseMessagingServiceEx extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        try {
            JSONObject sendBird = new JSONObject(remoteMessage.getData().get("sendbird"));
            JSONObject channel = (JSONObject) sendBird.get("channel");
            String channelUrl = (String) channel.get("channel_url");

            // Implement the following
            SendBird.markAsDelivered(channelUrl);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
Using an instance method
Light Color Skin
Copy
public class FirebaseMessagingServiceEx extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        try {
            JSONObject sendBird = new JSONObject(remoteMessage.getData().get("sendbird"));
            JSONObject channel = (JSONObject) sendBird.get("channel");
            String channelUrl = (String) channel.get("channel_url");

            GroupChannel.getChannel(channelUrl, new GroupChannel.GroupChannelGetHandler() {
                @Override
                public void onResult(GroupChannel channel, SendBirdException e){
                    if (channel != null) {
                        // implement the following
                        channel.markAsDelivered();
                    }
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void sendNotification(FirebaseMessagingServiceEx firebaseMessagingServiceEx, String message, String channelUrl) {
        // TODO: Send notifications here.
    }
}

Receive callbacks for delivery receipt

When a message is delivered to an online group channel member, it is automatically marked as delivered and the other online members are notified of delivery receipt through the onDeliveryReceiptUpdated() method in the channel event handler. However, when it is delivered to an offline group channel member as a push notification, the message can be marked as delivered through the groupChannel.markAsDelivered(), and other online members are notified of the delivery receipt through the onDeliveryReceiptUpdated().

Light Color Skin
Copy
SendBird.addChannelHandler(UNIQUE_HANDLER_ID, new SendBird.ChannelHandler() {
    @Override
    public void onMessageReceived(BaseChannel baseChannel, BaseMessage baseMessage) {
    }

    @Override
    public void onDeliveryReceiptUpdated(GroupChannel channel) {
    }
});

Retrieve number of members who haven’t received a message

You can retrieve the number of members who haven’t received a specific message in a group channel. If zero is returned, it means that the message has been delivered to all the other members.

Light Color Skin
Copy
SendBird.addChannelHandler(UNIQUE_HANDLER_ID, new SendBird.ChannelHandler() {
    @Override
    public void onMessageReceived(BaseChannel baseChannel, BaseMessage baseMessage) {
    }

    @Override
    public void onDeliveryReceiptUpdated(GroupChannel channel) {
        Boolean isAllDelivered = channel.getUndeliveredMemberCount(MESSAGE) == 0;
        ...

    }
});