Instant Messaging
Android
Getting Started
Introduction
Overview
Benefits
Features
Features
Account Integration and Login
Group Chat
Chat Room
Chat Room Tags
Multi-device Login
Statistics Monitoring
Limits
Integration
Interface Overview
Integrate and Initialize SDK
Messaging
Basic Messaging
Message History
Message Sending
Message Receiving
Recent Chats
Server Sessions
User Profile
User Relationship
Online Status Subscription
Push and Message Alert
Superteam
Chat Room
Moderation
Chat Extension
Miscellaneous Features
Best Practice
Traffic Control in Chat Rooms
Reference
API Reference
Status Codes

User Profile

Update time: 2021/12/03 17:18:12

CommsEase provides user profile hosting service, including user profile name, profile picture, birthday, email, gender, mobile phone number, signature, and extension field; which is hosted using UserInfo and its sub-interface NimUserInfo in SDK.

  • UserInfo interface desrcription:
Return Methods Description
String getAccount() Return the user account.
String getAvatar() Return the user profile picture address.
String getName() Return the user name.
  • NimUserInfo interface description:
Return Methods Description
String getBirthday() Get the birthday.
String getEmail() Get an Email.
String getExtension() Get an extension field.
Map getExtensionMap() Get an extension field, return Map format.
GenderEnum getGenderEnum() Get the gender.
String getMobile() Get a phone number.
String getSignature() Get a signature.

Getting user profiles

Getting user profiles on clients

To get the user profile, you must make an asynchronous call across processes. Developers can cache user profiles in the app, and access local cache to query user profiles. The SDK can inform registered observer of any changes in user profiles. At this time, the third-party app can update cache and refresh the interface.

Getting multiple user profiles at a time

Get user profiles at a time from the local database using user account set.

  • API prototype
/**
 * Get user profile list at a time from the local database (synchronous interface).
 *
 * @param accounts - User account set to be acquired.
 * @return - List of a user profile.
 */
List<NimUserInfo> getUserInfoList(List<String> accounts);
  • Parameters
Parameter Description
accounts User account set to be acquired.
  • Example
List<NimUserInfo> users = NIMClient.getService(UserService.class).getUserInfoList(accounts);

Getting a specified user profile

Get user profile from the local database using user account.

  • API prototype
/**
 * Get user profile from the local database (synchronous interface).
 *
 * @param account - User account to be acquired.
 * @return - User profile.
 */
NimUserInfo getUserInfo(String account);
  • Example
NimUserInfo user = NIMClient.getService(UserService.class).getUserInfo(account);

Getting all user profiles

Get all user profiles from the local database when creating user profile cache after login.

  • API prototype
/**
 * Get all user profile from the local database.
 *
 * @return - List of all user profile.
 */
List<NimUserInfo> getAllUserInfo();
  • Example
List<NimUserInfo> users = NIMClient.getService(UserService.class).getAllUserInfo();

Time to update a user profile

Except one's own local user profile, no warranties are made for the real-time update to other user profile. Other user profile update occasion:

  • Actively fetch and refresh user profile by calling the fetchUserInfo method.

  • Receive message from the user (If information about the message sender has any change, SDK will update and inform).

  • During data sync after login, SDK will automatically update friends' user profile.

Getting a user profile from cloud

User profile is acquired from the server and usually called when there is not local user profile. After acquisition, SDK will update local database. 150 users can be acquired at most each time. The upper level may get user profile in batch in the event of large quantity.

  • API prototype
/**
 * Get user profile from the server (The user profile of at most 150 users can be acquired each time. If the data size is large, upper developers can get such information at a time independently).
 *
 * @param accounts - User account to be acquired.
 * @return InvocationFuture - Configurable callback feature. It will be called back after user profile is stored in database.
 */
InvocationFuture<List<NimUserInfo>> fetchUserInfo(List<String> accounts);
  • Example
NIMClient.getService(UserService.class).fetchUserInfo(accounts)
   .setCallback(new RequestCallback<List<UserInfo>>() {... });

Notes: using the interface, user profile may be fetched from the server at a time. In consideration of user experience and flow cost, we do not recommend calling the interface frequently. For the pages under which the real-time performance requirement is not high, we do not recommend calling the interface of reading local cache.

Editing the user profile

  • API prototype
/**
 * Update your user profile.
 *
 * @param fields - Fields and new values to be updated. "key" is field, "value" is corresponding value.
 * @return @return InvocationFuture - It can set callback feature.
 */
InvocationFuture<Void> updateUserInfo(Map<UserInfoFieldEnum, Object> fields);
  • Parameters
Parameter Description
fields Fields and values to be update: "key" is field, "value" is corresponding value.

Input parameter Map<UserInfoFieldEnum, Object> and update user profile: "key" is field, "value" is corresponding value. See UserInfoFieldEnum for fields.

UserInfoFieldEnum attribute description:

Parameter Description
profile picture profile picture URL.
BIRTHDAY Birthday.
EMAIL Email.
EXTEND Extension field.It is recommended to use json.
GENDER Gender.
MOBILE Phone number.
Name Nickname.
SIGNATURE Signature.
undefined Undefined domain.
  • Format check for partial fields by SDK:
Field Description
Email Legal email.
Phone number Legal phone number, for example, 13588888888, +(86)-13055555555.
Birthday "yyyy-MM-dd" format is required.
  • Example
Map<UserInfoFieldEnum, Object> fields = new HashMap<>(1);
fields.put(UserInfoFieldEnum.Name, "new name");
NIMClient.getService(UserService.class).updateUserInfo(fields)
   .setCallback(new RequestCallbackWrapper<Void>() {... });

Profile picture processing

To update the profile picture, it is required to upload the profile picture image to the cloud, and transmit URL to SDK. You can use file uploading and downloading service in CommsEase or other third-party services.

The file uploading and downloading service in CommsEase is provided using interface NosService and NosServiceObserve. See Client API Document.

Callback for user profile changes

  • API prototype
/**
 * Observer notification for change of a user profile.
 *
 * @param observer - Observer. The parameter is list of updated user profile.
 * @param register true indicates registered, and true indicates unregistered.
 */
void observeUserInfoUpdate(Observer<List<NimUserInfo>> observer, boolean register);
  • Example
// Register or unregister the observer.
NIMClient.getService(UserServiceObserve.class).observeUserInfoUpdate(userInfoUpdateObserver, register);
// Observer for change of a user profile.
private Observer<List<UserInfo>> userInfoUpdateObserver = new Observer<List<UserInfo>>() {
	@Override
	public void onEvent(List<UserInfo> users) {
	...
	}
};

Search by profile name

Gets the user account list with specified display name from the local database.

  • API prototype
/**
 * Query account by display name backward.
 *
 * @param name - Nickname.
 * @return - Account.
 */
InvocationFuture<List<String>> searchAccountByName(String name);
  • Example
NIMClient.getService(UserService.class).searchAccountByName("nick").setCallback(new RequestCallbackWrapper<List<String>>() {
    @Override
    public void onResult(int code, List<String> result, Throwable exception) {
        if (code == ResponseCode.RES_SUCCESS) {
            // Success.
        } else {
            // Failure. See the code for error code.
        }

        if (exception != null) {
            // Error.
        }
    }
});

Search by keywords

Searches all users matched with keywords:

/**
 * Search all users matched with keywords.
 *
 * @param keyword - Keywords.
 * @return NimUserInfo.
 */
InvocationFuture<java.util.List<NimUserInfo>> searchUserInfosByKeyword(java.lang.String keyword);
Was this topic helpful?
Yes
No
  • Getting user profiles
  • Getting user profiles on clients
  • Getting multiple user profiles at a time
  • Getting a specified user profile
  • Getting all user profiles
  • Time to update a user profile
  • Getting a user profile from cloud
  • Editing the user profile
  • Profile picture processing
  • Callback for user profile changes
  • User search
  • Search by profile name
  • Search by keywords