Support

How do I show unread message count inside the UI of my app?

You can use the LYRQueryController with a LYRQuery that only fetches messages where isUnread is true, this would give you a count of all unread messages.


@property (nonatomic, retain) LYRQueryController *queryController;
...
LYRQuery *query = [LYRQuery queryWithQueryableClass:[LYRMessage class]];
query.predicate = [LYRPredicate predicateWithProperty:@"isUnread" predicateOperator:LYRPredicateOperatorIsEqualTo value:@(YES)];
self.queryController = [self.client queryControllerWithQuery:query error:nil];
self.queryController.delegate = self;

NSError *error;
BOOL success = [self.queryController execute:&error];
if (success) {
    NSLog(@"Query fetched %tu message objects", [self.queryController numberOfObjectsInSection:0]);
} else {
    NSLog(@"Query failed with error: %@", error);
}

Your interface has to implement the queryControllerDidChangeContent: delegate method. This method will get called every time the unread count will change:

- (void)queryControllerDidChangeContent:(LYRQueryController *)queryController
{
 self.numberOfUnreadMessagesLabel.text = [NSString stringWithFormat:@"%d", queryController.count];
}

But if you want to get the number of unread conversations, then you should setup the query withLYRConversation instead of LYRMessage:

LYRQuery *query = [LYRQuery queryWithQueryableClass:[LYRConversation class]];
Have more questions? Submit a request