Browse SDKs · iOS
Platform
SDKsiOS

Retrieve and maintain the total unread count

Retrieve and continuously maintain the account’s total unread count with OpenIM iOS SDK.

Copy

Query the aggregate unread message count across all conversations for the authenticated account using getTotalUnreadMsgCountWithOnSuccess:onFailure:. This method calculates the complete server-backed unread total, avoiding manual client-side tallying across paginated conversation lists.

Code example

[[OIMManager manager] getTotalUnreadMsgCountWithOnSuccess:^(NSInteger count) {
    [badgeStore setTotalUnread:count];
} onFailure:^(NSInteger code, NSString *message) {
    NSLog(@"getTotalUnreadMsgCount failed: %ld %@", (long)code, message);
}];

Return value

The onSuccess callback delivers an integer count representing the total unread message count across all conversations for the logged-in user.

State changes after the call

  • Local badge update: Update global tab bar badges or unread counter labels with the returned count.
  • Snapshot query: This query retrieves an instantaneous snapshot and does not emit delegate callbacks.
  • Asynchronous event listening: When new incoming messages arrive or conversations are marked as read, the SDK broadcasts the updated total unread count via onTotalUnreadMessageCountChanged: in OIMConversationListener:
@interface TotalUnreadSync : NSObject <OIMConversationListener>
@end

@implementation TotalUnreadSync
- (void)onTotalUnreadMessageCountChanged:(NSInteger)count {
    [badgeStore setTotalUnread:count];
}
@end

TotalUnreadSync *sync = [TotalUnreadSync new];
[[OIMManager callbacker] addConversationListener:sync];

// When logging out or tearing down view controllers:
[[OIMManager callbacker] removeConversationListener:sync];

To synchronize this count with the iOS system application icon badge, refer to Set the app badge count.