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 IUserInfoManager
to retrieve, set, and modify user attributes. Followings are the core methods:
UpdateOwnInfo
: Set and update user attributes.FetchUserInfoByUserId
: Retrieve the 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 user attributes:
// Set the user attributes of the specified user.
UserInfo userInfo;
userInfo.userId = currentId;
userInfo.nickName = "chatuser";
userInfo.avatarUrl = "http://www.easemob.com";
userInfo.birth = "2000.10.10";
userInfo.signature = "hello world";
userInfo.phoneNumber = "13333333333";
userInfo.email = "123456@qq.com";
userInfo.gender = 1; // 1 indicates that the gender is male. 2 means female.
SDKClient.Instance.UserInfoManager.UpdateOwnInfo(userInfo, new CallBack(
onSuccess: () => {
},
onError:(code, desc) => {
}
));
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:
// Retrieve user attributes of one or multiple users.
List<string> idList = new List<string>();
idList.Add("username");
SDKClient.Instance.UserInfoManager.FetchUserInfoByUserId
SDKClient.Instance.UserInfoManager.FetchUserInfoByUserId(idList, type, startId, loadCount, new ValueCallBack<Dictionary<string, UserInfo>>(
// `result` is in the format of Dictionary<string, UserInfo>
onSuccess: (result) => {
// Traverse all the user attributes in the dictionary
},
onError:(code, desc) => {
}
));
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 userCard
.userID
, nickname
, and avatarUrl
as fileds in ext
. Send the custom message.Followings are the sample code for creating and sending a namecard message:
string event = "userCard";
Dictionary<string, string> adict = new Dictionary<string, string>();
adict.Add("userId", userInfo.userId);
adict.Add("nickname", userInfo. nickname);
adict.Add("avatarUrl", userInfo.avatarUrl);
// You can add more fields.
// Create a custom message.
Message msg = Message.CreateCustomSendMessage(toChatUsername, event, adict);
// Send the message.
SDKClient.Instance.ChatManager.SendMessage(ref msg, new CallBack(
onSuccess: () => {
Debug.Log($"{msg.MsgId}发送成功");
},
onError: (code, desc) => {
Debug.Log($"{msg.MsgId}发送失败,errCode={code}, errDesc={desc}");
}
));