Subscribing to User Online Status
This document guides developers on implementing user online status subscription, querying, and monitoring in the RC IM Android Client SDK. Through this documentation, Android developers will learn how to obtain and track user online status, as well as receive notifications when status changes occur.
This feature is supported starting from version 5.8.1.
Service Requirements
This feature is only available with the Chat Pro Plan. Once you purchase the Pro Plan, this feature is automatically available without requiring separate activation.
Listening to Subscription Events
To promptly receive notifications about subscription event changes, you need to set up an online status subscription listener after initializing the IMLib SDK but before establishing the IMLib SDK connection.
When handling subscription event change notifications, you should process the corresponding business logic based on the SubscribeType in the callback method. The online subscription status type is ONLINE_STATUS.
- Starting from version 5.10.0,
SubscribeTypeincludes two types: online status subscriptionONLINE_STATUS(1)and user profile hostingUSER_PROFILE(2). - Starting from version 5.12.0,
SubscribeTypeadds two new types: friend online status subscriptionFRIEND_ONLINE_STATUS(3)and friend user profile hostingFRIEND_USER_PROFILE(4).
RongCoreClient.getInstance().addSubscribeEventListener(new OnSubscribeEventListener() {
/**
* @param subscribeEvents List of subscription events containing all changed events.
* When a subscribed user's status changes, SubscribeEvent.operationType has no value.
* No notification is sent when a subscription expires; developers need to monitor expiration times themselves.
* Note: Check the SubscribeType of SubscribeInfoEvent - ONLINE_STATUS indicates online status.
*/
@Override
public void onEventChange(List<SubscribeInfoEvent> subscribeEvents) {
}
/**
* Subscription data synchronization completed.
* This method is called after subscription data is successfully synchronized to the device or system, for subsequent processing.
* @deprecated Deprecated. Use {@link #onSubscriptionSyncCompleted(SubscribeEvent.SubscribeType)} instead.
*/
@Override
public void onSubscriptionSyncCompleted() {
}
/**
* Marks subscription data synchronization as complete. Called after subscription data is successfully synchronized to the device or system, for subsequent processing.
* Note: Check SubscribeType - ONLINE_STATUS indicates online status.
*
* @param type Type of completed synchronization. Determine the specific business logic based on this type.
* @since 5.10.0
*/
@Override
public void onSubscriptionSyncCompleted(SubscribeEvent.SubscribeType type) {
}
/**
* Called when a user's subscription information changes on another device.
* This can be used to update the user's status on the current device, ensuring consistency of subscription information.
* Note: Check the SubscribeType of SubscribeInfoEvent - ONLINE_STATUS indicates online status.
* @param subscribeEvents List of subscription events
*/
@Override
public void onSubscriptionChangedOnOtherDevices(List<SubscribeEvent> subscribeEvents) {
}
});
Subscribing to User Online Status
To track the online status of specific users, you need to use the subscribeEvent method for subscription.
- Create a subscription request object
SubscribeEventRequest, set the subscription type to online statusSubscribeEvent.SubscribeType.ONLINE_STATUS, and configure the list of user IDs to be subscribed along with the subscription duration.
- The maximum number of users that can be subscribed to in a single operation is 200.
- The total subscription limit is 1000 users.
- Subscription duration: Define an integer value representing the subscription duration, ranging from 60 seconds to 2592000 seconds.
- A single user can be subscribed to by a maximum of 5000 users.
Sample Code
//Set subscription type
SubscribeEvent.SubscribeType type= SubscribeEvent.SubscribeType.ONLINE_STATUS;
//Set subscription duration in seconds (range: [60,2592000])
int expiry=180000;
//List of user IDs to subscribe to (maximum 200 per operation)
List<String> userList=new ArrayList<>();
userList.add("user1");
userList.add("user2");
userList.add("user3");
SubscribeEventRequest request = new SubscribeEventRequest(type,expiry,userList);
RongCoreClient.getInstance().subscribeEvent(request, new IRongCoreCallback.SubscribeEventCallback<List<String>>() {
@Override
public void onSuccess() {
//Subscription successful
}
@Override
public void onError(int errorCode, List<String> strings) {
//Subscription failed
}
});
Batch Querying Online Status of Subscribed Users
Batch query the online status of subscribed users. After enabling the Client Friend Online Status Change Notification feature, you can query the current online status of specified friends for the current user.
This feature is supported starting from version 5.28.0.
List<String> userIds = new ArrayList<>();
userIds.add("user1");
userIds.add("user2");
userIds.add("user3");
RongCoreClient.getInstance().getSubscribeUsersOnlineStatus(userIds, new IRongCoreCallback.ResultCallback<List<SubscribeUserOnlineStatus>>() {
@Override
public void onSuccess(List<SubscribeUserOnlineStatus> subscribeUserOnlineStatuses) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
}
});
Unsubscribing from User Online Status
When you no longer need to track a user's online status, you can use the unSubscribeEvent method to unsubscribe.
//Set unsubscription type
SubscribeEvent.SubscribeType type= SubscribeEvent.SubscribeType.ONLINE_STATUS;
//List of user IDs to unsubscribe from (maximum 200 per operation)
List<String> userList=new ArrayList<>();
userList.add("user1");
userList.add("user2");
userList.add("user3");
SubscribeEventRequest request = new SubscribeEventRequest(type,userList);
RongCoreClient.getInstance().unSubscribeEvent(request, new IRongCoreCallback.SubscribeEventCallback<List<String>>() {
@Override
public void onSuccess() {
//Unsubscription successful
}
@Override
public void onError(int errorCode, List<String> strings) {
//Unsubscription failed
}
});
Querying Subscription Status Information for Specified Users
You can use the querySubscribeEvent method to query status information for specified users and subscription types. A maximum of 200 users' status information can be queried in a single operation.
//Set query type
SubscribeEvent.SubscribeType type= SubscribeEvent.SubscribeType.ONLINE_STATUS;
//Query user online status
List<String> userList=new ArrayList<>();
userList.add("user1");
userList.add("user2");
userList.add("user3");
SubscribeEventRequest request = new SubscribeEventRequest(type,userList);
RongCoreClient.getInstance().querySubscribeEvent(request, new IRongCoreCallback.ResultCallback<List<SubscribeInfoEvent>>() {
@Override
public void onSuccess(List<SubscribeInfoEvent> subscribeInfoEvents) {
//Query successful, returns queried user information
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
//Query failed
}
});
Paginated Query of Subscribed Users' Status Information
If you need to paginate through all subscribed event status information, you can use the querySubscribeEvent method and specify the page size and starting index.
-
Parameter pageSize: Page size [1~200].
-
Parameter startIndex: Pass 0 for the first page. For subsequent pages, pass the total count of returned data (e.g., if pageSize = 20, pass 20 for the second page and 40 for the third page).
//Query type
SubscribeEvent.SubscribeType type= SubscribeEvent.SubscribeType.ONLINE_STATUS;
SubscribeEventRequest request = new SubscribeEventRequest(type);
//Page size, valid range [1,200].
int pageSize=20;
//Starting index for pagination. Pass 0 for the first page. For subsequent pages, pass the total count of returned data (e.g., if pageSize = 20, pass 20 for the second page and 40 for the third page).
int startIndex=0;
RongCoreClient.getInstance().querySubscribeEvent(request,pageSize,startIndex, new IRongCoreCallback.ResultCallback<List<SubscribeInfoEvent>>() {
@Override
public void onSuccess(List<SubscribeInfoEvent> subscribeInfoEvents) {
//Query successful, returns queried user information
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
//Query failed
}
});