Skip to main content

User attributes

After joining a Chat channel, a user can update information such as the nickname, avatar, age, and mobile phone number as needed. These are known as user attributes.

This page shows how to use the Chat SDK to implement managing user attributes.

  • User attributes are stored on the Chat server. If you have security concerns, Agora recommends that you manage user attributes yourself.
  • To ensure information security, app users can only modify their own user attributes. Only app admins can modify the user attributes of other users.

Understand the tech

The Chat SDK uses UserInfoManager to retrieve, set, and modify user attributes. Followings are the core methods:

  • updateOwnInfo: Set or update user attributes.
  • updateOwnInfoByAttributes: Set or update the specified user attribute.
  • fetchUserInfoByUserId: Retrieve the user attributes of the specified user.
  • fetchUserInfoByAttributes: Retrieve the specified user attributes of the specified user.

Prerequisites

Before proceeding, ensure that you meet the following requirements:

  • You have integrated the Chat SDK, initialized the SDK and implemented the functionality of registering accounts and login. For details, see Chat SDK quickstart.
  • Have a thorough understanding of the API call frequency limit, the maximum size of all the attributes of a specified user, and the maximum size of all user attributes in an app. For details, see Known limitations.

Implementation

This section shows how to manage user attributes and contacts with the methods provided by the Chat SDK.

Set user attributes

Chat users can set and update their own attributes. Refer to the code example to set all the user attributes:


_19
// Call updateOwnInfo to set all the user attributes
_19
UserInfo userInfo = new UserInfo();
_19
userInfo.setUserId(ChatClient.getInstance().getCurrentUser());
_19
userInfo.setNickname("agora");
_19
userInfo.setAvatarUrl("http://www.agora.io");
_19
userInfo.setBirth("2000.10.10");
_19
userInfo.setSignature("hello world");
_19
userInfo.setPhoneNumber("13333333333");
_19
userInfo.setEmail("123456@qq.com");
_19
userInfo.setGender(1);
_19
ChatClient.getInstance().userInfoManager().updateOwnInfo(userInfo, new ValueCallBack<String>() {
_19
@Override
_19
public void onSuccess(String value) {
_19
}
_19
_19
@Override
_19
public void onError(int error, String errorMsg) {
_19
}
_19
});

The following sample code uses avatar as an example to show how to set the specified user attribute:


_11
// Call updateOwnInfoByAttribute to update the specified attribute of the specified user
_11
String url = "https://download-sdk.oss-cn-beijing.aliyuncs.com/downloads/IMDemo/avatar/Image1.png";
_11
ChatClient.getInstance().userInfoManager().updateOwnInfoByAttribute(UserInfoType.AVATAR_URL, url, new ValueCallBack<String>() {
_11
@Override
_11
public void onSuccess(String value) {
_11
}
_11
_11
@Override
_11
public void onError(int error, String errorMsg) {
_11
}
_11
});

Retrieve user attributes

You can use fetchUserInfoByUserId to retrieve the user attributes of the specified users. For each method call, you can retrieve the user attributes of a maximum of 100 users.

Refer to the following code example:


_5
// Call fetchUserInfoByUserId to retrieve all the attributes of the specified user.
_5
String[] userId = new String[1];
_5
//username indicates the user ID.
_5
userId[0] = username;
_5
ChatClient.getInstance().userInfoManager().fetchUserInfoByUserId(userId, new ValueCallBack<Map<String, UserInfo>>() {});


_8
// Call fetchUserInfoByAttribute to retrieve the specified user attribute
_8
String[] userId = new String[1];
_8
userId[0] = ChatClient.getInstance().getCurrentUser();
_8
UserInfoType[] userInfoTypes = new UserInfoType[2];
_8
userInfoTypes[0] = UserInfoType.NICKNAME;
_8
userInfoTypes[1] = UserInfoType.AVATAR_URL;
_8
ChatClient.getInstance().userInfoManager().fetchUserInfoByAttribute(userId, userInfoTypes,
_8
new ValueCallBack<Map<String, UserInfo>>() {});

Next steps

This section introduces extra functions you can implement in your app using user attributes and contact management.

Manage user avatar

The Chat SDK only supports storing the URL address of the avatar file rather than the file itself. To manage user avatars, you need to use a third-party file storage service.

To implement user avatar management in your app, take the following steps:

  1. Upload the avatar file to the third-party file storage service. Once the file is successfully uploaded, you get a URL address of the avatar file.
  2. Set the avatarUrl parameter in user attributes as the URL address of the avatar file.
  3. To display the avatar, call fetchUserInfoByUserId to retrieve the URL of the avatar file, and then render the image on the local UI.

Create and send a namecard using user attributes

Namecard messages are custom messages that include the user ID, nickname, avatar, email address, and phone number of the specified user. To create and send a namecard, take the following steps:

  1. Create a custom message and set the event of the custom message as USER_CARD_EVENT.
  2. Add userId, getNickname, and getAvatarUrl as fileds in params. Send the custom message.

Followings are the sample code for creating and sending a namecard message:


_12
// Creates a cutom message
_12
ChatMessage message = ChatMessage.createSendMessage(ChatMessage.Type.CUSTOM);
_12
CustomMessageBody body = new CustomMessageBody(DemoConstant.USER_CARD_EVENT);
_12
Map<String,String> params = new HashMap<>();
_12
params.put(DemoConstant.USER_CARD_ID,userId);
_12
params.put(DemoConstant.USER_CARD_NICK,user.getNickname());
_12
params.put(DemoConstant.USER_CARD_AVATAR,user.getAvatarUrl());
_12
body.setParams(params);
_12
message.setBody(body);
_12
message.setTo(toUser);
_12
// Sends the custom message
_12
ChatClient.getInstance().chatManager().sendMessage(message);