Unread Count

This section would help you in getting the unread count in different scenarios. You can use this count for a lot of use cases like showing total count in the icon's badge and in a lot of different scenarios.

Individual user

To get the user's chat unread count with any user, use the below code:

ALContactService* contactService = [ALContactService new];
 ALContact *contact = [contactService loadContactByKey:@"userId" value:<userID>];
 NSNumber *unreadCount = [contact unreadCount];
let contactService = ALContactService()
let contact = contactService.loadContact(byKey: "userId", value: <userId>)
let unreadCount = contact?.unreadCount

Individual Group

To get the user's chat unread count in any group, use the below code:

ALChannelService *channelService = [ALChannelService new];
  ALChannel *alChannel = [channelService getChannelByKey:<channelKey>];
  NSNumber *unreadCount = [alChannel unreadCount];
let channelService = ALChannelService()
let channel = channelService.getChannelByKey(<channelKey>)
let unreadCount = channel?.unreadCount

Total unread messages

To get the user's total unread count i.e Users + Groups, use the below code:

ALMessageService *service = [[ALMessageService alloc] init];
[service getTotalUnreadMessageCountWithCompletionHandler:^(NSUInteger unreadCount, NSError *error) {

      if (error){
            NSLog(@"ERRROR in fetching count");

     NSLog(@"Total unread message count @ :%lu",(unsigned long)unreadCount);
let service = ALMessageService()
 service.getTotalUnreadMessageCount { unreadcount, error in
    guard error == nil else {
          print("failed to get the unread count");
    print("Total unread message count is", unreadcount)

Total unread conversations

To get the total unread conversations use the below method

ALMessageService * service = [[ALMessageService alloc] init];
[service getTotalUnreadConversationCountWithCompletionHandler:^(NSUInteger unreadCount, NSError *error) {

   if (error){
       NSLog(@"ERRROR in fetching getTotalUnreadConversationCountWithCompletionHandler");

    NSLog(@"Current unread conversation count is  @@ :%lu",(unsigned long)unreadCount);
let service = ALMessageService()
    service.getTotalUnreadConversationCount { unreadcount, error in
    guard error == nil else {
          print("Failed to get the unread count");
     print("Unread conversation count is", unreadcount)

Real time update of count

  • To update unread message count real time for any incoming messages, you can observe below notification with name:NEW_MESSAGE_NOTIFICATION
[[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(Your handler method)
  • Stop observing notification (You can add this in -(void)viewWillDisappear)
[[NSNotificationCenter defaultCenter] removeObserver:self name:NEW_MESSAGE_NOTIFICATION object:nil];

Show badge count

To show the badge count on Application icon you need to add the below line in ALDefaultChatViewSettings method in the ALChatManager file

[ALUserDefaultsHandler setUnreadCountType:1];

What’s Next
Did this page help you?