Manage chat groups
Chat groups enable real-time messaging among multiple users.
This page shows how to use the Chat SDK to create and manage a chat group in your app.
Understand the tech
The Chat SDK provides the Group
, GroupManager
, and GroupChangeListener
classes for chat group management, which allows you to implement the following features:
- Create and destroy a chat group
- Join and leave a chat group
- Retrieve the member list of a chat group
- Block and unblock a chat group
- Listen for the chat group events
Prerequisites
Before proceeding, ensure that you meet the following requirements:
- You have initialized the Chat SDK. For details, see Get Started with Android.
- You understand the call frequency limits of the Chat APIs supported by different pricing plans as described in Limitations.
- You understand the number of chat groups and chat group members supported by different pricing plans as described in Pricing Plan Details.
Implementation
This section describes how to call the APIs provided by the Chat SDK to implement chat group features.
Create and destroy a chat group
Users can create a chat group and set the chat group attributes such as the name, description, group members, and reasons for creating the group. Users can also set the GroupOptions
parameter to specify the size and type of the chat group. Once a chat group is created, the creator of the chat group automatically becomes the chat group owner.
Only chat group owners can disband chat groups. Once a chat group is disbanded, all members of that chat group receive the onGroupDestroyed
callback and are immediately removed from the chat group. All local data for the chat group is also removed from the database and memory.
Refer to the following sample code to create and destroy a chat group:
Join and leave a chat group
Users can request to join a public chat group as follows:
- Call
getPublicGroupsFromServer
to retrieve the list of public groups by page. Users can obtain the ID of the group that they want to join. - Call
joinGroup
to send a join request to the chat group:- If the type of the chat group is set to
GroupStylePublicJoin
, the request from the user is accepted automatically and the other chat group members receive theonMemberJoined
callback. - If the type of the chat group is set to
GroupStylePublicNeedApproval
, the chat group owner and chat group admins receive theonRequestToJoinReceived
callback and determine whether to accept the request from the user.
- If the type of the chat group is set to
Users can call leaveGroup
to leave a chat group. Once a user leaves the group, all the other group members receive the onMemberExited
callback.
Refer to the following sample code to join and leave a chat group:
Retrieve the member list of a chat group
To retrieve the member list of a chat group, choose the method based on the group size:
- If the members of a chat group are greater than or equal to 200, list members of the chat group by page.
- If the members of a chat group are less than 200, call
getGroupFromServer
to retrieve the member list of the chat group.
Refer to the following sample code to retrieve the member list of a chat group:
Block and unblock a chat group
All chat group members can block and unblock a chat group. Once a member block a chat group, they no longer receive messages from this chat group.
Refer to the following sample code to block and unblock a chat group:
Listen for chat group events
To monitor the chat group events, users can listen for the callbacks in the GroupManager
class and add app logics accordingly. If a user wants to stop listening for the callbacks, make sure that the user removes the listener to prevent memory leakage.
Refer to the following sample code to listen for chat group events: