Track and subscribe to user online/offline status and presence updates in your Android app
AI Integration Quick Reference
Kotlin
Java
// Subscribe to presence during initval appSettings = AppSettings.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion("REGION") .build()CometChat.init(context, "APP_ID", appSettings, callback)// Add user listenerCometChat.addUserListener("LISTENER_ID", object : CometChat.UserListener() { override fun onUserOnline(user: User?) { Log.d(TAG, "${user?.name} is online") } override fun onUserOffline(user: User?) { Log.d(TAG, "${user?.name} is offline") }})// Remove listener (important!)CometChat.removeUserListener("LISTENER_ID")
// Subscribe to presence during initAppSettings appSettings = new AppSettings.AppSettingsBuilder() .subscribePresenceForAllUsers() .setRegion("REGION") .build();CometChat.init(context, "APP_ID", appSettings, callback);// Add user listenerCometChat.addUserListener("LISTENER_ID", new CometChat.UserListener() { @Override public void onUserOnline(User user) { Log.d(TAG, user.getName() + " is online"); } @Override public void onUserOffline(User user) { Log.d(TAG, user.getName() + " is offline"); }});// Remove listener (important!)CometChat.removeUserListener("LISTENER_ID");
User Presence lets you know if a user is available to chat. Configure presence subscription during init(), then listen for online/offline events via UserListener.
Based on the AppSettings configured during init(), the logged-in user receives presence updates for other users.AppSettingsBuilder provides three subscription options:
subscribePresenceForAllUsers() — Receive presence for all users in the app.
subscribePresenceForRoles(List<String> roles) — Receive presence only for users with specified roles.
subscribePresenceForFriends() — Receive presence only for friends.
If none are set, no presence events are sent.Register UserListener in onResume() and remove it in onPause().
val listenerID:String = "UNIQUE_LISTENER_ID"CometChat.removeUserListener(listenerID)
Always remove listeners when they’re no longer needed (e.g., in onPause() or onDestroy()). Failing to remove listeners can cause memory leaks and duplicate event handling.