Support

How do I get the number of unread messages in a conversation?

On iOS you would run a query like this:

LYRQuery *query = [LYRQuery queryWithClass:[LYRMessage class]];
LYRPredicate *conversationPredicate = [LYRPredicate predicateWithProperty:@"conversation" operator:LYRPredicateOperatorIsEqualTo value:conversation];
LYRPredicate *unreadPredicate = [LYRPredicate predicateWithProperty:@"isUnread" operator:LYRPredicateOperatorIsEqualTo value:@(YES)];
LYRPredicate *userPredicate = [LYRPredicate predicateWithProperty:@"sender.userID" operator:LYRPredicateOperatorIsNotEqualTo value:self.client.authenticatedUserID];
query.predicate = [LYRCompoundPredicate compoundPredicateWithType:LYRCompoundPredicateTypeAnd subpredicates:@[conversationPredicate, unreadPredicate, userPredicate]];

NSError *error;
NSUInteger count = [self.client countForQuery:query error:&error];
if (!error) {
    NSLog(@"%tu unread messages in conversation", count);
} else {
    NSLog(@"Query failed with error %@", error);
}

On Android, the query would look like this:

Query query = Query.builder(Message.class)
    .predicate(new CompoundPredicate(CompoundPredicate.Type.AND,
        new Predicate(Message.Property.CONVERSATION, Predicate.Operator.EQUAL_TO, conversation),
        new Predicate(Message.Property.IS_UNREAD, Predicate.Operator.EQUAL_TO, true)))
    .build();
List<Integer> resultArray = layerClient.executeQuery(query, Query.ResultType.COUNT); int count = resultArray.get(0).intValue();

System.out.println(count + " unread messages in conversation");
Have more questions? Submit a request