User information
Chat UI SDK supports two methods for displaying user information:
- Nexconn-hosted method: This is the default method. Chat UI SDK retrieves hosted user, group, and group member information through Nexconn APIs. See Profile and relationship management.
- User information provider delegate method (current document): Use this method if your app does not want to host profile data in Nexconn. Application developers must actively retrieve user information, group information, and group member user information from the app server and provide it to the SDK. The user nickname and avatar passed when registering to obtain a token from the Nexconn server are only used for push services.
Chat UI SDK uses the NCUserInfoManager class to uniformly manage the following data. Your app needs to use NCUserInfoManager to provide data to Chat UI SDK for display in the UI.
- User information: includes nickname and avatar
- Group information: includes group name and group avatar
- Group member user information: only supports group user nicknames
This document describes how the application layer provides user nicknames and avatars (UserInfo) to the Chat UI SDK.
UserInfo is provided by Chat SDK (ai.nexconn.chat:chat) and used by Chat UI. Use matching chat and chatui versions, especially when integrating local AAR files such as nexconn-chat_<version>.aar and nexconn-chatui_<version>.aar.
By default, NCUserInfoManager uses DataSourceType.INFO_MANAGEMENT. In this mode, the SDK uses Nexconn-hosted profile APIs. If your app does not want to host profile data in Nexconn, switch to DataSourceType.INFO_PROVIDER; the SDK then reads user data from the provider and local cache. See Profile and relationship management.
UserInfo parameters
The examples in this document use the UserInfo constructor to provide a user ID, display name, and avatar URL to Chat UI SDK.
UserInfo userInfo = new UserInfo(userId, 0, name, portraitUri);
UserInfo supports the following parameters:
| Parameter | Type | Description |
|---|---|---|
| userId | String | User ID. Use the same user ID that appears in messages and channels. |
| userType | int | User type. 0 represents a user. Chat UI SDK does not use this value for display logic; your app can define other values for its own business logic. |
| name | String | Display name or nickname shown in Chat UI. Provide it if you want Chat UI to display a name instead of falling back to a user ID or empty value. |
| portraitUri | String | Avatar URL string shown in Chat UI. |
| alias | String | Alias or remark name. If set, Chat UI uses alias before name when displaying a user name. |
| extra | String | Extension field stored with the user information. Chat UI SDK does not assign a fixed meaning to this field. |
If your app only needs to provide the user ID, nickname, and avatar, use 0 for userType and use the four-parameter constructor shown above. If you need to provide alias or extra, use the six-parameter constructor:
UserInfo userInfo = new UserInfo(userId, 0, name, portraitUri, alias, extra);
Refresh user information
If the app locally holds user information data (for example, the nickname and avatar of the currently logged-in user), you can directly refresh the user information (avatar and nickname) stored in the local cache and database. After refreshing, the Chat UI SDK UI will automatically display the latest user information (UserInfo).
Refreshing user information must be performed after Chat UI SDK has successfully established an IM connection; otherwise, local data cannot be refreshed. Possible applicable scenarios include:
- When the app starts for the first time and successfully establishes an IM connection, you can batch-provide the user information required by your business to the SDK, which will write it to the cache and local database for subsequent use.
- After the IM connection is established, if user nickname, avatar, or other information changes, the app server notifies the client (for example, using messages), and the client calls the interface to refresh user information.
UserInfo userInfo = new UserInfo(userId, 0, "Display name for userId", "https://example.com/avatar.png");
NCUserInfoManager.getInstance().refreshUserInfoCache(userInfo);
If the app does not hold data locally, we recommend dynamically providing user information when Chat UI SDK needs to display data.
Dynamically provide user information
The SDK provides a user information provider interface class UserDataProvider.UserInfoProvider. If Chat UI SDK cannot obtain user information from NCUserInfoManager, it will trigger the getUserInfo callback method of UserInfoProvider. The app should provide the user avatar and nickname required by the SDK in this callback. After obtaining user information data, the SDK will automatically set and refresh the user avatar and nickname, and implement related UI display.
Set user information provider to provide user information to the SDK
During the application lifecycle, after initialization and before connection, call the setUserInfoProvider method of NCUserInfoManager to set the user information provider. If the SDK has obtained user information and allows persistent storage (isCacheUserInfo is true), it will cache the information in memory. The SDK prioritizes obtaining user information from the local database, and the data will still be available when the app starts next time. The SDK's default behavior when processing corresponding information is as follows:
- When the SDK needs to display user information in the UI, Chat UI SDK will first call the
getUserInfomethod ofNCUserInfoManagerone by one based on the user ID to query already obtained data from the cache or local database. - If the SDK can query the required information from the cache or local database, it will directly return the data to the UI layer and refresh the UI.
- If the SDK cannot query the required information from the cache or local database, it will trigger the callback method of
UserDataProvider.UserInfoProviderand attempt to obtain information from the application layer. After receiving the corresponding information provided by the application layer, the SDK will refresh the UI.
We recommend setting isCacheUserInfo to true, which means persistently storing user information locally.
Provide user information data to the SDK when the getUserInfo callback of UserDataProvider.UserInfoProvider is triggered.
-
Asynchronously obtain user information, then manually refresh: The app can use this method to avoid time-consuming operations affecting the UI.
-
The app needs to directly return
nullin thegetUserInfomethod. At the same time, the app should trigger its own logic to obtain user information foruserIdin this method. Note that this step will temporarily set the user information foruserIdto empty.JavaNCUserInfoManager.getInstance().setUserInfoProvider(new UserDataProvider.UserInfoProvider() {
@Override
public UserInfo getUserInfo(String userId) {
// When user information is needed, such as on the channel list or channel page,
// ChatUI calls getUserInfo for each user ID.
// The app handles the asynchronous user information request here, then provides
// the result to the SDK through refreshUserInfoCache.
return null;
}
}, true); -
After the app successfully obtains user information data for
userId, call therefreshUserInfoCachemethod ofNCUserInfoManagerto manually refresh the user information foruserId. The SDK will refresh the UI after receiving the user's information. See Refresh user information for details.JavaUserInfo userInfo = new UserInfo(userId, 0, "Display name for userId", "https://example.com/avatar.png");
NCUserInfoManager.getInstance().refreshUserInfoCache(userInfo);
-
-
Synchronously return user information: The app can also directly return user information for
userId. The SDK will refresh the UI after receiving the user's information.JavaNCUserInfoManager.getInstance().setUserInfoProvider(new UserDataProvider.UserInfoProvider() {
@Override
public UserInfo getUserInfo(String userId) {
UserInfo userInfo = new UserInfo(userId, 0, "Display name for userId", "https://example.com/avatar.png");
return userInfo;
}
}, true);
Get user information
The app can actively call the getUserInfo method of NCUserInfoManager to obtain user information cached by the SDK. The SDK's behavior is as follows:
- First, it attempts to obtain data provided by the application layer from the local cache. If the SDK was authorized to store user information in the local database when setting the user information provider (i.e.,
isCacheUserInfoistrue), the SDK will also attempt to obtain user information from the local database. - If there is no data for the relevant information locally, the SDK will trigger the
getUserInfocallback method ofUserInfoProvider. If your app layer has provided data in this callback, the SDK can successfully obtain user information (UserInfo).
String userId = "user_id";
UserInfo userInfo = NCUserInfoManager.getInstance().getUserInfo(userId);
| Input parameter | Type | Description |
|---|---|---|
| userId | String | User ID |