Unread Count

This section would help you in getting the unread count in different scenarios.

Unread Count in UIUnread Count in UI

Unread Count in UI

Individual User

To get the unread count of individual contact pass the userId as below:

int contactUnreadCount = new MessageDatabaseService(context).getUnreadMessageCountForContact(userId);
val contactUnreadCount = MessageDatabaseService(context).getUnreadMessageCountForContact(userId)

Individual Group

To get the unread count of individual channel/group pass the channelKey as below:

int channelUnreadCount = new MessageDatabaseService(context).getUnreadMessageCountForChannel(channelKey);
val channelUnreadCount = MessageDatabaseService(context).getUnreadMessageCountForChannel(channelKey)

Total

To get the total unread count:

int totalUnreadCount = new MessageDatabaseService(context).getTotalUnreadCount();
val totalUnreadCount = MessageDatabaseService(context).totalUnreadCount

Real-time update of count

To update unread message count real time for any incoming messages, you will receive MobiComKitConstants.APPLOZIC_UNREAD_COUNT broadcast. Define below broadcast in your activity.

BroadcastReceiver unreadCountBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (MobiComKitConstants.APPLOZIC_UNREAD_COUNT.equals(intent.getAction())) {
                                  MessageDatabaseService messageDatabaseService = new MessageDatabaseService(context);
                 int unreadCount  =  messageDatabaseService.getTotalUnreadCount();
                    //Update unread count in UI
                }
            }
        };
val unreadCountBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                
                if (MobiComKitConstants.APPLOZIC_UNREAD_COUNT == intent.action) {
                    val messageDatabaseService = MessageDatabaseService(context)
                    val unreadCount = messageDatabaseService.totalUnreadCount
                    //Update unread count in UI
                }
                
            }
 }

Register the broadcast

You can register the broadcast in your activity or any other place where you want to use it

LocalBroadcastManager.getInstance(this).registerReceiver(unreadCountBroadcastReceiver, new IntentFilter(MobiComKitConstants.APPLOZIC_UNREAD_COUNT));
LocalBroadcastManager.getInstance(this).registerReceiver(unreadCountBroadcastReceiver,  IntentFilter(MobiComKitConstants.APPLOZIC_UNREAD_COUNT));

Unregister the broadcast

You need to unregister the broadcast once the activity is closed or app use the below code

LocalBroadcastManager.getInstance(this).unregisterReceiver(unreadCountBroadcastReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(unreadCountBroadcastReceiver);

What’s Next
Did this page help you?