Group Channel: Advanced

This section explains the advanced features of Group Channel. Some of them are the premium features available only to a paying user.


Add users of an application as friends

To find and chat with other users within the application easily, users can add friends by using unique identifiers such as hashed phone numbers for privacy. When the unique identifier, which is used as the discovering keys, linked to a user using our service is uploaded, we can match and retrieve the users and then automatically register them as friends.

//In case of uploading friend discovery keys
Map<String, String> discoveryMap = new HashMap<>();
discoveryMap.put("0001-0002-0003", "John");
discoveryMap.put("0004-0005-0006", "Jay");
discoveryMap.put("0007-0008-0009", "Jin");

SendBird.uploadFriendDiscoveries(discoveryMap, new SendBird.UploadFriendDiscoveriesHandler() {
    @Override
    public void onResult(SendBirdException e) {    // Error.
        return;
    }
});

//In case of deleting friend discovery keys
List<String> discoveryKeys = new ArrayList<>();
discoveryKeys.add("0001-0002-0003");
discoveryKeys.add("0004-0005-0006");
discoveryKeys.add("0007-0008-0009");

SendBird.deleteFriendDiscoveries(discoveryKeys, new SendBird.DeleteFriendDiscoveriesHandler() {
    @Override
    public void onResult(SendBirdException e) {    // Error.
        return;
    }
});

Or you can make any user add and delete friends by using the userIds of others like below.

List<String> userIds = new ArrayList<>();
userIds.add("Harry");

// In case of adding friends
SendBird.addFriends(userIds, new SendBird.AddFriendsHandler() {
    @Override
    public void onResult(List<User> list, SendBirdException e) {    // Error.
        return;        
    }
});


// In case of deleting friends
SendBird.deleteFriends(userIds, new SendBird.DeleteFriendsHandler() {
    @Override
    public void onResult(List<User> list, SendBirdException e) {    // Error.
        return;        
    }
});

Also you can retrieve a list of friends of a certain user by creating a FriendListQuery. The next() method returns a list of User objects.

FriendListQuery friendListQuery = SendBird.createFriendListQuery();
friendListQuery.next(new FriendListQuery.FriendListQueryResultHandler() {
    @Override
    public void onResult(List<User> list, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Send typing indicators to other members in a group channel

Calling startTyping() and endTyping() when a user is typing will invoke a onTypingStatusUpdate() in the channel event handler.

groupChannel.startTyping();
groupChannel.endTyping();
...

SendBird.addChannelHandler(UNIQUE_HANDLER_ID, new SendBird.ChannelHandler() {
    @Override
    public void onTypingStatusUpdated(GroupChannel groupChannel) {
        if (currentGroupChannel.getUrl().equals(groupChannel.getUrl())) {
            List<User> members = groupChannel.getTypingMembers();
            // Refresh typing status of members within channel.
        }
    }
});

Mark messages as read in a group channel

Calling markAsRead() when a member reads messages will invoke a onReadReceiptUpdated() in the channel event handlers of all members within and then make the channel up-to-date.

groupChannel.markAsRead();
...

SendBird.addChannelHandler(UNIQUE_HANDLER_ID, new SendBird.ChannelHandler() {
    @Override
    public void onReadReceiptUpdated(GroupChannel groupChannel) {

    }
});

Retrieve the read receipt of a message in a group channel

You can check the number of members in a group channel who have not read a message using getReadReceipt(). To get the exact value, the channel should be updated by markAsRead() before calling the getReadReceipt().

groupChannel.markAsRead();
...

SendBird.addChannelHandler(UNIQUE_HANDLER_ID, new SendBird.ChannelHandler() {
    ...

    @Override
    public void onReadReceiptUpdated(GroupChannel groupChannel) {
        if (currentGroupChannel.getUrl().equals(groupChannel.getUrl())) {
            ...    

            for (BaseMessage msg : yourMessages) {
                int unreadCount = groupChannel.getReadReceipt(msg);
                if (unreadCount <= 0) { 
                    // All members have read the message.
                } else {
                    // Some of members haven't read the message.
                }
            }
        }
    }
});

Retrieve members who have read a message

Using getReadMembers(), you can view members who have read a message in a group channel. A list of the members for each message is depending on each member's read receipt. For keeping track of who have read a new message, you should pass the newly received message as a parameter to getReadMembers() within onReadReceiptUpdated() which is called when other member has read a message.

List<User> readMembers = groupChannel.getReadMembers(MESSAGE);

The getReadMembers() returns a list of members who have read a message except the current user and the message sender.

Note: Similarly, using getUnreadMembers(), you can retreive a list of members who have not read a message except the current user and the message sender. And getReadStatus() returns a list of all members' user information except the current user.


Retrieve the last message of a group channel

You can view the last message of a group channel.

BaseMessage lastMessage = groupChannel.getLastMessage();

Retrieve count of unread messages in a group channel

You can retrieve count of unread messages of a certain user in a group channel.

int unreadMessageCount = groupChannel.getUnreadMessageCount();

Retrieve total count of unread messages in group channels

You can retrieve total count of unread messages of a certain user in all joined group channels.

GroupChannel.getTotalUnreadMessageCount(new GroupChannel.GroupChannelTotalUnreadMessageCountHandler() {
    @Override
    public void onResult(int totalUnreadMessageCount, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Retrieve total count of group channels which have unread messages

You can retrieve total count of group channels which have unread messages of a certain user.

GroupChannel.getTotalUnreadChannelCount(new GroupChannel.GroupChannelTotalUnreadChannelCountHandler() {
    @Override
    public void onResult(int totalUnreadChannelCount, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Send admin messages to a group channel

You can send admin messages to users in a channel using the SendBird Dashboard or the Platform API. To send an admin message via the Dashboard, in the Group Channels panel, select a group channel, find the message box below, click the Admin Message tab, and write the message in the box. An admin message is limited to 1000 characters.

If you are currently developing under the Free Plan and therefore cannot use the Moderation Tools from the Dashboard, you must send the admin messages through the Platform API.


Categorize group channels by custom types

When creating a channel, you can specify additional custom types to further subclassify your channels. This custom type takes on the form of a String, and can be useful in searching or filtering channels.

Note: DATA and CUSTOM_TYPE are both String fields that allow you to append information to your channels. The use case is for CUSTOM_TYPE to contain information that can subclassify the channel (for example, categorizing into School and Work channels). However, both these fields can be flexibly utilized.

GroupChannel.createChannel(NAME, COVER_URL, DATA, CUSTOM_TYPE new OpenChannel.OpenChannelCreateHandler() {
    @Override
    public void onResult(GroupChannel groupChannel, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

To get a channel's custom type, call channel.getCustomType().


Categorize messages in a group channel by custom types

Likewise, you can specify additional custom types for messages to categorize them into more specific groups. This custom type takes on the form of a String, and can be useful in searching or filtering messages.

Note: DATA and CUSTOM_TYPE are both String fields that allow you to append information to your messages. The use case is for CUSTOM_TYPE to contain information that can subclassify the message (for example, categorizing into FILE_IMAGE and FILE_AUDIO type messages). However, both these fields can be flexibly utilized.

To embed a custom type into a message, pass a String parameter to sendUserMessage() or sendFileMessage().

channel.sendUserMessage(MESSAGE, DATA, CUSTOM_TYPE, new BaseChannel.SendUserMessageHandler() {
    @Override
    public void onSent(UserMessage userMessage, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

To get a message's custom type, call message.getCustomType().


Search for group channels by name and URLs

You can search for specific channels by adding keywords to GroupChannelListQuery instance. There are two types of keywords: a Name and a URL. A query returns a list of group channels that partially match the specified Name keyword in the names of the channels.

myChannelListQuery = GroupChannel.createMyGroupChannelListQuery();
myChannelListQuery.setIncludeEmpty(true);
myChannelListQuery.setChannelNameContainsFilter("SendBird");

myChannelListQuery.next(new GroupChannelListQuery.GroupChannelListQueryResultHandler() {
    @Override
    public void onResult(List<OpenChannel> channels, SendBirdException e) {
        if (e != null) {    // Error!
            return;
        }
        // A list of group channels that have "SendBird" in their names is returned.
    }
});

A query returns a list of group channels that partially match the specified URL keywords in the urls of the channels.

myChannelListQuery = GroupChannel.createMyGroupChannelListQuery();
myChannelListQuery.setIncludeEmpty(true);

List<String> channelUrls = new ArrayList<>();
channelUrls.add("seminar");
channelUrls.add("lecture");

myChannelListQuery.setChannelUrlsFilter(channelUrls);

myChannelListQuery.next(new GroupChannelListQuery.GroupChannelListQueryResultHandler() {
    @Override
    public void onResult(List<OpenChannel> channels, SendBirdException e) {
        if (e != null) {    // Error!
            return;
        }
        // A list of group channels that have "seminar" or "lecture" in their urls is returned.
    }
});

Generate thumbnails of a file message

When sending an image file, you can choose to create thumbnails of the image, which you can fetch and render into your UI. You can specify up to 3 different dimensions to generate thumbnail images in, which can be convenient for supporting various display densities.

Note: Supported file types are files whose media type is image/* or video/*. The SDK does not support creating thumbnails when sending a file message via a file URL.

Create a list of FileMessage.ThumbnailSize objects to pass to channel.sendFileMessage(). A ThumbnailSize can be created with the constructor ThumbnailSize(width, height), where the values specify pixels. The onSent() callback subsequently returns a list of Thumbnail objects that each contain the URL of the generated thumbnail image file.

List<FileMessage.ThumbnailSize> thumbnailSizes = new ArrayList<>();

// Create and add ThumbnailSizes (Maximum number of thumbnails: 3).
thumbnailSizes.add(new ThumbnailSize(100, 100));
thumbnailSizes.add(new ThumbnailSize(200, 200));

channel.sendFileMessage(FILE, NAME, TYPE, SIZE, DATA, CUSTOM_TYPE, thumbnailSizes, new BaseChannel.SendFileMessageHandler() {
    public void onSent(FileMessage fileMessage, SendBirdException e) {
        if (e != null) {    // Error!
            return;
        }

        Thumbnail first = fileMessage.getThumbnails.get(0);
        Thumbnail second = fileMessage.getThumbnails.get(1);

        int maxHeightFirst = first.getMaxHeight(); // 100
        int maxHeightSecond = second.getMaxHeight(); // 200

        String urlFirst = first.getUrl(); // URL of first thumbnail file.
        String urlSecond = second.getUrl(); // URL of second thumbnail file.
    }
 });

As this is one of SendBird's Premium Features, please contact our sales team for further assistance.


Share an encrypted file with other members

This feature encrypts all file messages sent by a channel member with a master key when saved on SendBird server. This encrypted file has its own access URL, and only member with the master key can decrypt the file and open it.

As this is one of SendBird's Premium Features, please contact our sales team for further assistance. This feature will also require customized implementation efforts from your side.


Spam flood protection

This feature allows you to customize the number of messages a member can send in a group channel per second. By doing so, all excess messages will be deleted and only the number of messages allowed to be sent per member per second will be delivered. This feature protects your application from some members spamming others in the channel with the same messages.

Note: Our default system setting is 5 messages per second. This limit can be manually adjusted only by our side. You can contact our engineering team for further assistance on this setting. As this is one of SendBird's Premium Features, please contact our sales team for further assistance.


Message auto-translation

SendBird makes it possible for messages to be sent in different languages through its auto-translation feature. Pass in a List of language codes to sendUserMessage() to request translated messages in the corresponding languages.

ArrayList targetLangList = new ArrayList<String>();
targetLangList.add("es");
targetLangList.add("ko");

channel.sendUserMessage(MESSAGE, DATA, CUSTOM_TYPE, targetLangList, new BaseChannel.SendUserMessageHandler() {
    @Override
    public void onSent(UserMessage userMessage, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

You can obtain translations of a message using userMessage.getTranslations() which returns a Map containing the language codes and translations.

SendBird.addChannelHandler(UNIQUE_CHANNEL_ID, new SendBird.ChannelHandler() {
    @Override
    public void onMessageReceived(BaseChannel baseChannel, BaseMessage baseMessage) {
        Map<String, String> map = ((UserMessage) baseMessage).getTranslations();
        String esTranslation = map.get("es");
        ...
        // Display translation in UI.
    }
});

Note: The message auto-translation supports 53 languages. For the language code table, see the Miscellaneous > Supported Languages. As this is one of SendBird's Premium Features, please contact our sales team for further assistance.