Browse SDKs · Android
Platform
SDKsAndroid

Subscribe to user status

Subscribe to the online presence and multi-platform login state of specified users.

Copy

Subscribe to real-time online status and multi-platform connection state for specified users. When subscription succeeds, the SDK delivers a snapshot of their current presence immediately and publishes incremental event updates whenever target users log in, disconnect, or change active platforms.

Code example

List<String> userIDs = Arrays.asList("user_a", "user_b");

OpenIMClient.getInstance().userInfoManager.subscribeUsersOnlineStatus(
    new OnBase<List<UsersOnlineStatus>>() {
        @Override
        public void onSuccess(List<UsersOnlineStatus> states) {
            // Render online presence indicators using states
        }

        @Override
        public void onError(int code, String error) {
            // Handle error
        }
    },
    userIDs
);

Parameters

ParameterTypeRequiredDescription
baseOnBase<List<UsersOnlineStatus>>YesCallback delivering the asynchronous operation result.
userIDsList<String>YesList of target user IDs to subscribe to. Subscribe on demand when entering a chat or list view.

Return value

The successful callback returns an initial presence snapshot as List<UsersOnlineStatus>. Maintain your presence cache using userID as the unique key:

FieldDescription
userIDTarget user ID.
statusPresence status (1 for online, 0 for offline).
platformIDsList of active client platforms (such as Android, iOS, or Web platform codes).

State changes after the call

Subsequent presence transitions for subscribed users arrive via OnUserListener:

@Override
public void onUserStatusChanged(UsersOnlineStatus status) {
    // Received status update; update the UI matching status.getUserID()
}

When navigating away from a chat screen or when presence tracking is no longer needed, call Unsubscribe from user status to conserve bandwidth and avoid unnecessary presence broadcasts.