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

Manage chat group member attributes

Chat groups enable real-time messaging among multiple users.

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

  • Set custom attributes of a group member via key and value items.
  • Fetch group member custom attributes.
  • Listen for attribute changes of a group member.

Prerequisites

Before proceeding, ensure that you meet the following requirements:

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

Set custom attributes of a group member via key and value items

Each chat group member can set their own attributes. Chat group admins/owners can also modify all members' attributes. Each custom attribute should be in key-value format.

Refer to the following sample code to set a custom attribute of a group member:


_11
Map<String,String> attributeMap = new HashMap<>();
_11
attributeMap.put("nickName",nickName);
_11
_11
ChatClient.getInstance().groupManager().asyncSetGroupMemberAttributes(groupId, userId, attributeMap, new CallBack() {
_11
@Override
_11
public void onSuccess() {
_11
}
_11
@Override
_11
public void onError(int code, String error) {
_11
}
_11
});

Fetch group member custom attributes

Chat group members and group admins/owners can retrieve custom attributes of multiple group members by attribute key.

Refer to the following sample code to use the attribute key to fetch custom attributes of multiple group members:


_24
List<String> keyList = new ArrayList<>();
_24
keyList.add("nickName");
_24
_24
List<String> userIds = new ArrayList<>();
_24
userIds.add("Tom");
_24
userIds.add("Jack");
_24
_24
ChatClient.getInstance().groupManager().asyncFetchGroupMembersAttributes(groupId, userIds, keyList, new ValueCallBack<Map<String, Map<String, String>>>() {
_24
@Override
_24
public void onSuccess(Map<String, Map<String, String>> value) {
_24
if (value != null){
_24
for (String user : userIds) {
_24
Map<String,String> map = value.get(user);
_24
if (map != null){
_24
//……
_24
}
_24
}
_24
}
_24
}
_24
_24
@Override
_24
public void onError(int code, String error) {
_24
}
_24
});

Listen for attribute changes of a group member

GroupChangeListener class holds callbacks that can be used to monitor the change of any key-value items. When such a change occurs, an onGroupMemberAttributeChanged callback will notify the Client SDK by returning chat group ID, UID, and key-value pairs of the changes.


_15
//Create a GroupChangeListener object
_15
GroupChangeListener groupChangeListener = new GroupChangeListener() {
_15
@Override
_15
public void onGroupMemberAttributeChanged(String groupId, String userId, Map<String, String> attribute, String from) {
_15
if ( attribute != null && attribute.size() > 0){
_15
//EMLog.d(TAG,"onGroupMemberAttributeChanged: " + groupId +" - "+ attribute.toString());
_15
}
_15
}
_15
};
_15
_15
//Add a group change listener:
_15
ChatClient.getInstance().groupManager().addGroupChangeListener(groupChangeListener);
_15
_15
//Remove a group change listener:
_15
ChatClient.getInstance().groupManager().removeGroupChangeListener(groupChangeListener);

vundefined