Skip to main content
Android
iOS
Web
Windows
Unity
Flutter
React Native

Manage chat group members

Chat groups enable real-time messaging among multiple users.

This page shows how to use the Chat SDK to manage the members of 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:

  • Add and remove users from a chat group
  • Manage the owner and admins of a chat group
  • Manage the block list of a chat group
  • Manage the mute list of a chat group
  • Mute and unmute all the chat group members
  • Manage the allow list of a chat group

Prerequisites

Before proceeding, ensure that you meet the following requirements:

  • You have initialized the Chat SDK. For details, see SDK quickstart.
  • 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.

Manage chat group members

  1. Add users to a chat group.
    Whether a chat group is public or private, the chat group owner and chat group admins can add users to the chat group. As for private groups, if the type of a chat group is set to GroupStylePrivateMemberCanInvite, group members can invite users to join the chat group.

  2. Implement chat group invitations.

    Does a group invitation require consent from an invitee to add them to the group (inviteNeedConfirm):

    • Yes (option.InviteNeedConfirm is set totrue). After creating a group and sending group invitations, the subsequent logic varies based on whether an invitee automatically consents to the group invitation (autoAcceptGroupInvitation):

      • Yes (autoAcceptGroupInvitation is set to true). The invitee automatically joins the chat group and receives the GroupChangeListener#onAutoAcceptInvitationFromGroup callback, the inviter receives the GroupChangeListener#onInvitationAccepted and GroupChangeListeneronMemberJoined callbacks, and the other chat group members receive the GroupChangeListener#onMemberJoined callback.
      • No (autoAcceptGroupInvitation is set to false). The invitee receives the GroupChangeListener#onInvitationReceived callback and chooses whether to join the chat group:
        • If the invitee accepts the group invitation, the inviter receives the GroupChangeListener#onInvitationAccepted and GroupChangeListener#onMemberJoined callbacks and the other chat group members receive the GroupChangeListener#onMemberJoined callback;
        • If the invitee declines the group invitation, the chat group owner receives the GroupChangeListener#groupInvitationDidDecline callback.
    • No (option.InviteNeedConfirm is set to false). After creating a chat group and sending group invitations, an invitee is added to the chat group regardless of their autoAcceptGroupInvitation setting. The invitee receives the GroupChangeListener#onAutoAcceptInvitationFromGroup callback, the inviter receives the GroupChangeListener#onInvitationAccepted and GroupChangeListener#onMemberJoined callbacks and the other chat group members receive the GroupChangeListener#onMemberJoined callback.

  3. Remove chat group members from a chat group.
    The chat group owner and chat group admins can remove chat group members from a chat group, whereas chat group members do not have this privilege. Once a group member is removed, the removed member receives the onUserRemoved callback and all other members in the group receive the onMemberExited callback.

Refer to the following sample code to add and remove a user:


_20
// The chat group owner and chat group admins can call addUsersToGroup to add users to a chat group.
_20
ChatClient.getInstance().groupManager().addUsersToGroup(groupId, newmembers);
_20
_20
// Chat group members can call inviteUser to invite users to a chat group.
_20
ChatClient.getInstance().groupManager().inviteUser(groupId, newmembers, null);
_20
_20
// The chat group owner and chat group admins can call removeUserFromGroup to remove a member from a chat group.
_20
ChatClient.getInstance().groupManager().removeUserFromGroup(groupId, username);
_20
// The chat group owner and chat group admins can call asyncRemoveUsersFromGroup to remove members from a chat group.
_20
ChatClient.getInstance().groupManager().asyncRemoveUsersFromGroup("GroupId", userList, new CallBack() {
_20
@Override
_20
public void onSuccess() {
_20
_20
}
_20
_20
@Override
_20
public void onError(int code, String error) {
_20
_20
}
_20
});

Manage chat group ownership and admin

  1. Transfer the chat group ownership. The chat group owner can transfer ownership to the specified chat group member. Once ownership is transferred, the original chat group owner becomes a group member. The new owner receives the onOwnerChanged callback.

  2. Add chat group admins.
    The chat group owner can add admins. Once added to the chat group admin list, the newly added admin and the other chat group admins receive the onAdminAdded callback.

  3. Remove chat group admins.
    The chat group owner can remove admins. Once removed from the chat group admin list, the removed admin and the other chat group admins receive the onAdminRemoved callback.

Refer to the following sample code the manage chat group ownership and admin:


_8
// The chat group owner can call changeOwner to transfer ownership to the specified chat group member.
_8
ChatClient.getInstance().groupManager().changeOwner(groupId, newOwner);
_8
_8
// The chat group owner can call `addGroupAdmin` to add admins.
_8
ChatClient.getInstance().groupManager().addGroupAdmin(groupId, admin);
_8
_8
// The chat group owner can call `removeGroupAdmin` to remove admins.
_8
ChatClient.getInstance().groupManager().removeGroupAdmin(groupId, admin);

Manage the chat group block list

The chat group owner and chat group admins can add or remove the specified member to the chat group block list. Once a chat group member is added to the block list, this member cannot send or receive chat group messages, nor can this member join the chat group again.

Refer to the following sample code to manage the chat group block list:


_8
// The chat group owner and admins can call blockUser to add the specified member to the chat group block list.
_8
ChatClient.getInstance().groupManager().blockUser(groupId, username);
_8
_8
// The chat group owner and admins can call unblockUser to remove the specified member from the chat group block list.
_8
ChatClient.getInstance().groupManager().unblockUser(groupId, username);
_8
_8
// The chat group owner and admins can call getBlockedUsers to retrieve the chat group block list.
_8
ChatClient.getInstance().groupManager().getBlockedUsers(groupId);

Manage the chat group mute list

The chat group owner and chat group admins can add or remove the specified member to the chat group mute list. Once a chat group member is added to the mute list, this member can no longer send chat group messages, not even after being added to the chat group allow list.

Refer to the following sample code to manage the chat group mute list:


_9
// The chat group owner and admins can call muteGroupMember to add the specified member to the chat group mute list. The muted member and all the other chat group admins or owner receive the onMuteListAdded callback.
_9
// duration: The mute duration. If you pass `-1`, members are muted permanently.
_9
ChatClient.getInstance().groupManager().muteGroupMembers(groupId, muteMembers, duration);
_9
_9
// The chat group owner and admins can call unmuteGroupMember to remove the specified user from the chat group mute list. The unmuted member and all the other chat group admins or owner receive the onMuteListRemoved callback.
_9
ChatClient.getInstance().groupManager().unMuteGroupMembers(String groupId, List<String> members);
_9
_9
// The chat group owner or admin can call fetchGroupMuteList to retrieve the chat group mute list.
_9
ChatClient.getInstance().groupManager().fetchGroupMuteList(String groupId, int pageNum, int pageSize);

Mute and unmute all the chat group members

The chat group owner and chat group admins can mute or unmute all the chat group members. Once all the members are muted, only those in the chat group allow list can send messages in the chat group.

Unlike muting a chat group member, this kind of mute does not expire automatically after a certain period and you need to call the unmuteAllMembers method to unmute all members in the chat group.

Refer to the following sample code to mute and unmute all the chat group members:


_5
// The chat group owner or admin can call muteAllMembers to mute all the chat group members. Once all the members are muted, these members receive the onAllMemberMuteStateChanged callback.
_5
public void muteAllMembers(final String groupId, final ValueCallBack<Group> callBack);
_5
_5
// The chat group owner or admin can call unmuteAllMembers to unmute all the chat group members. Once all the members are unmuted, these members receive the onAllMemberMuteStateChanged callback.
_5
public void unmuteAllMembers(final String groupId, final ValueCallBack<Group> callBack);

Manage the chat group allow list

The chat group owner and admins are added to the chat group allow list by default.

Members in the chat group allow list can send chat group messages even when the chat group owner or admin has muted all chat group members. However, if a member is already in the chat group mute list, adding this member to the allow list does not take effect.

Refer to the following sample code to manage the chat group allow list:


_11
// The chat group owner or admin can call addToChatGroupWhiteList to add the specified member to the chat group allow list. Once the member is added, all the other chat group admins or owner receive the onWhiteListAdded callback.
_11
public void addToGroupWhiteList(final String groupId, final List<String> members, final CallBack callBack);
_11
_11
// The chat group owner or admin can call removeFromChatGroupWhiteList to remove the specified member from the chat group list. Once the member is removed, all the other chat group admins or owner receive the onWhiteListRemoved callback.
_11
public void removeFromGroupWhiteList(final String groupId, final List<String> members, final CallBack callBack);
_11
_11
// Chat group members can call checkIfInChatGroupWhiteList to check whether they are in the chat group allow list.
_11
public void checkIfInGroupWhiteList(final String groupId, ValueCallBack<Boolean> callBack);
_11
_11
// The chat group owner or admin can call fetchChatGroupWhiteList to retrieve the chat group allow list.
_11
public void fetchGroupWhiteList(final String groupId, final ValueCallBack<Dictionary<string>> callBack);

Listen for chat group events

For details, see Chat Group Events.

vundefined