After joining an Agora 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 Agora Chat SDK to implement managing user attributes.
The Agora 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.Before proceeding, ensure that you meet the following requirements:
This section shows how to manage user attributes and contacts with the methods provided by the Agora Chat SDK.
Chat users can set and update their own attributes. Refer to the code example to set all the user attributes:
// Call updateOwnInfo to set all the user attributes
UserInfo userInfo = new UserInfo();
userInfo.setUserId(ChatClient.getInstance().getCurrentUser());
userInfo.setNickname("agora");
userInfo.setAvatarUrl("http://www.agora.io");
userInfo.setBirth("2000.10.10");
userInfo.setSignature("hello world");
userInfo.setPhoneNumber("13333333333");
userInfo.setEmail("123456@qq.com");
userInfo.setGender(1);
ChatClient.getInstance().userInfoManager().updateOwnInfo(userInfo, new ValueCallBack<String>() {
@Override
public void onSuccess(String value) {
}
@Override
public void onError(int error, String errorMsg) {
}
});
The following sample code uses avatar as an example to show how to set the specified user attribute:
// Call updateOwnInfoByAttribute to update the specified attribute of the specified user
String url = "https://download-sdk.oss-cn-beijing.aliyuncs.com/downloads/IMDemo/avatar/Image1.png";
ChatClient.getInstance().userInfoManager().updateOwnInfoByAttribute(UserInfoType.AVATAR_URL, url, new ValueCallBack<String>() {
@Override
public void onSuccess(String value) {
}
@Override
public void onError(int error, String errorMsg) {
}
});
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:
// Call fetchUserInfoByUserId to retrieve all the attributes of the specified user.
String[] userId = new String[1];
//username指的是用户id
userId[0] = username;
ChatClient.getInstance().userInfoManager().fetchUserInfoByUserId(userId, new ValueCallBack<Map<String, UserInfo>>() {});
// Call fetchUserInfoByAttribute to retrieve the specified user attribute
String[] userId = new String[1];
userId[0] = ChatClient.getInstance().getCurrentUser();
UserInfoType[] userInfoTypes = new UserInfoType[2];
userInfoTypes[0] = UserInfoType.NICKNAME;
userInfoTypes[1] = UserInfoType.AVATAR_URL;
ChatClient.getInstance().userInfoManager().fetchUserInfoByAttribute(userId, userInfoTypes,
new ValueCallBack<Map<String, UserInfo>>() {});
This section introduces extra functions you can implement in your app using user attributes and contact management.
The Agora 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:
avatarUrl
parameter in user attributes as the URL address of the avatar file.fetchUserInfoByUserId
to retrieve the URL of the avatar file, and then render the image on the local UI.Namecard messages are custom messages that include the user ID, nickname, avator, email address, and phone number of the specified user. To create and send a namecard, take the following steps:
event
of the custom message as USER_CARD_EVENT
.userId
, getNickname
, and getAvatarUrl
as fileds in params
. Send the custom message.Followings are the sample code for creating and sending a namecard message:
// Creates a cutom message
ChatMessage message = ChatMessage.createSendMessage(ChatMessage.Type.CUSTOM);
CustomMessageBody body = new CustomMessageBody(DemoConstant.USER_CARD_EVENT);
Map<String,String> params = new HashMap<>();
params.put(DemoConstant.USER_CARD_ID,userId);
params.put(DemoConstant.USER_CARD_NICK,user.getNickname());
params.put(DemoConstant.USER_CARD_AVATAR,user.getAvatarUrl());
body.setParams(params);
message.setBody(body);
message.setTo(toUser);
// Sends the custom message
ChatClient.getInstance().chatManager().sendMessage(message);