Skip to main content

User Information

To display user avatars, nicknames, etc., on the Global IM UIKit UI, the application layer (App) must actively provide user information (UserInfo) to the Global IM UIKit SDK.

Global IM UIKit uses the UserInfoConfig class to uniformly manage the following data. The App needs to use UserInfoConfig to provide data to Global IM UIKit for display on the UI.

  • User Information: Includes nickname and avatar
  • Group Information: Includes group name and group avatar
  • Group Member User Information: Only supports group member nicknames

User information, group information, and group member user information must be actively obtained by the application developer from the App server and provided to the SDK. RC does not provide App user and group information hosting services. The user nicknames and avatars on the RC server are only used for push notifications.

This document only describes how the application layer (App) provides user nicknames and avatars (UserInfo) to the Global IM UIKit SDK:

UserInfo userInfo = new UserInfo(userId, "Name corresponding to userId", Uri.parse("Avatar URL corresponding to userId"));

Refresh User Information

If the App locally holds user information data (e.g., the nickname and avatar of the currently logged-in user), it can directly refresh the user information (avatar and nickname) stored in the local cache and database. After refreshing, the Global IM UIKit UI will display the latest user information UserInfo.

Refreshing user information must be done after Global IM UIKit has successfully established an IM connection; otherwise, local data cannot be refreshed. Possible scenarios include:

  • After the App is launched for the first time and successfully establishes an IM connection, it can provide the SDK with the user information required by its business in bulk. The SDK will then write this information into the cache and local database for future use.
  • After the IM connection is established, if the user's nickname, avatar, etc., change, the App server notifies the client (e.g., via a message), and the client calls the interface to refresh the user information.
UserInfo userInfo = new UserInfo(userId, "Name corresponding to userId", Uri.parse("Avatar URL corresponding to userId"));
ConfigCenter.getUserInfoConfig().refreshUserInfoCache(userInfo);

If the App does not hold data locally, it is recommended to dynamically provide user information when Global IM UIKit needs to display the data.

Dynamically Provide User Information

Starting from Global IM UIKit 5.X, the SDK has designed the "User Information Provider" UserDataProvider.UserInfoProvider interface class. If Global IM UIKit cannot obtain user information from UserInfoConfig, it will trigger the getUserInfo callback method of UserInfoProvider. The App should provide the SDK with the required user avatar and nickname in this callback.

After obtaining the user information data, the SDK will automatically set and refresh the user avatar and nickname, as well as implement the relevant UI display.

public interface UserInfoProvider {
/**
* Get user information.
*
* @param userId User ID.
* @return User information.
*/
UserInfo getUserInfo(String userId);
}

Step 1: Set User Information Provider

Use the ConfigCenter.getUserInfoConfig().setUserInfoProvider() method to set the user information provider. This must be done after SDK initialization and before establishing an IM connection. It is recommended to set this during the application lifecycle.

// Allow the SDK to persistently store user information locally
boolean isCacheUserInfo = true;

ConfigCenter.getUserInfoConfig().setUserInfoProvider(new UserDataProvider.UserInfoProvider() {
@Override
public UserInfo getUserInfo(String userId) {
...// The App needs to provide user information here.
}
}, isCacheUserInfo);
ParameterTypeDescription
userInfoProviderUserDataProvider.UserInfoProviderUser information provider interface
isCacheUserInfobooleanWhether to persistently store user information in the SDK's local database. true means store. false means do not store.

It is recommended to set isCacheUserInfo to true, i.e., persistently store user information locally.

During the App's lifecycle, if the SDK has obtained user information, it will cache this information in memory. If persistent storage is allowed, the SDK will first retrieve user information from the local database, and the data will still be available the next time the App starts. The SDK's default behavior when processing corresponding information is as follows:

  1. When the SDK needs to display user information on the UI, it first queries the data already obtained from memory.
  2. 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.
  3. If the SDK cannot query the required information from the cache or local database, it will trigger the callback method of UserDataProvider.UserInfoProvider and attempt to obtain the information from the application layer. After receiving the corresponding information provided by the application layer, the SDK will refresh the UI.

Step 2: Provide User Information to the SDK

When user information needs to be displayed (e.g., on the conversation list page or conversation page), Global IM UIKit will first call the getUserInfo method of UserInfoConfig one by one based on user IDs to obtain user information. If the SDK cannot query the required user information from the cache or local database, it will trigger the callback method of UserDataProvider.UserInfoProvider, requesting the App to provide user information data.

Please 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.

    1. The App needs to directly return null in the getUserInfo method. At the same time, the App should trigger the logic to obtain the user information of userId on its own in this method. Note that this step will temporarily set the user information of userId to empty.

      ConfigCenter.getUserInfoConfig().setUserInfoProvider(new UserDataProvider.UserInfoProvider {
      @Override
      public UserInfo getUserInfo(String userId) {
      ...// When user information needs to be displayed (e.g., on the conversation list page or conversation page), Global IM UIKit will first call getUserInfo one by one based on user IDs.
      // The App should complete the logic of asynchronously requesting user information here. The information is later provided to the SDK via refreshUserInfoCache.
      return null;
      }

      }, true);
    2. After the App successfully obtains the user information data of userId, it calls the refreshUserInfoCache method of UserInfoConfig to manually refresh the user information of userId. The SDK will refresh the UI after receiving this user's information. See Refresh User Information for details.

      UserInfo userInfo = new UserInfo(userId, "Name corresponding to userId", Uri.parse("Avatar URL corresponding to userId"))
      ConfigCenter.getUserInfoConfig().refreshUserInfoCache(userInfo);
  • Synchronously Return User Information: The App can also directly return the user information of userId. The SDK will refresh the UI after receiving this user's information.

    ConfigCenter.getUserInfoConfig().setUserInfoProvider(new UserDataProvider.UserInfoProvider() {
    @Override
    public UserInfo getUserInfo(String userId) {
    UserInfo userInfo = new UserInfo(userId, "Name corresponding to userId", Uri.parse("Avatar URL corresponding to userId"))
    return userInfo;
    }
    }, true);