简体   繁体   中英

Quickblox iOS - Messages Not Read

I'm having an issue with messages not being marked as read in Quickblox, even though the messages are indeed being read.

Is there an easy way to mark all messages in a dialog as read in the most recent Quickblox iOS SDK?

Quickblox does not handle read status of messages automatically on it's own :) In order to maintain the read status of the messages client applications has to co-operate as well :)

In order to receive the read status all the messages you send out for which you expect the read status has to be marked as markable :)

Here is a sample code from QB docs

QBChatMessage *message = [QBChatMessage markableMessage];
[message setText:@"Hi there!"];
[message setRecipientID:1022725];

Assume that user 1 sends out Hi there to User 2 :) Now the code in User 2's device should inform that user 2 has seen or read the message to quickblox server :) On informing the read status to Quickblox server fro a specific QBChatMessage, QB server sends out a notification to User 1 telling him that User 2 Has read the message :)

On receiving message User 2 should inform QB server about the status using

if([message markable]){
        [[QBChat instance] readMessage:message completion:^(NSError * _Nullable error) {

        }];
    }

Once User 2 informs QB server about the read status, QB server sends out notification to User1 acknowledging the same, which can be captured using :)

- (void)chatDidReadMessageWithID:(NSString *)messageID dialogID:(NSString *)dialogID readerID:(NSUInteger)readerID
{

}

Once receiving the read status for a particular QBChatMessage you can update the UI to show the seen status for that message :)

TIP

Now as you might have noticed, read status works per message basis, this can be very much irritating and lead to complicated logic to send read status to all the messages one by one when you receive the whole lot of messages using

[QBRequest messagesWithDialogID:@"54fda689535c125b0700bbfa" extendedRequest:nil forPage:resPage successBlock:^(QBResponse *response, NSArray *messages, QBResponsePage *responcePage) {

} errorBlock:^(QBResponse *response) {
    NSLog(@"error: %@", response.error);
}];

So simple solution is :

Rather than sending out the read status to all the messages in an array one by one you can send out the seen message only to last the message when user taps on the dialog to see the messages inside it :)

Once the user enters the chats screen all the message belonging to that dalog will anyway be considered as read (Thats how all the messaging apps including whatsApp behaves)

So sending seen status only to the last message makes sense :) And on receiving the seen status for a particular message you change the seen status to all the messages with sent date less than the message which received the seen status :)

EDIT

Anyway All being said there are scenarios where in which you just need to send the read status to all the messages belonging to a particular dialog as your unread message count depends on the read status as well :)

For example if you want to update the unread message count for a particular dialog as 0 from 100 so its not advicable to send seen status to all 100 :)

You can make use of this API. Docs clearly says (and I am using it in my app as well ) you can set the read status to all messages belonging to a particular dialog just dont send the message id :)

Here is the API

curl -X PUT \
-H "Content-Type: application/json" \
-H "QB-Token: eddf864695d72d33b959eec2ae6c640d817dfada" \
-d '{"read": "1", "chat_dialog_id": "53a99a7be4b094c7c6d31b41"}' \
https://api.quickblox.com/chat/Message/53aabe15e4b077ddd43e7fd3.json

Here is the link to docs :) http://quickblox.com/developers/Chat#Update_message

All you have to do is to

1> make a web service call to api " https://api.quickblox.com/chat/Message "

Notice 53aabe15e4b077ddd43e7fd3 is your message id :) If you want to send the seen status to all messages belonging to a particular dialog dont append it to URL :)

2>In header field send Content-Type: application/json and QB-Token: current_user_token

3>and in body please send read=1 and chat_dialog_id = dialog_id :)

Thats it all the messages belonging to this dialog will be updated as read and hence the unread message count of the dialog will be 0 :)

NSURL *url = [NSURL URLWithString:@"https://api.quickblox.com/chat/Message"];
NSMutableURLRequest *mutableRequest=[NSMutableURLRequest requestWithURL:url];
[mutableRequest setHTTPMethod:@"PUT"];
[mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[mutableRequest setValue:[QBSession currentSession].sessionDetails.token forHTTPHeaderField:@"QB-Token"];
NSString *jsonString=[NSString stringWithFormat:@"{\"read\": \"1\", \"chat_dialog_id\": \"%@\"}",dialogToSendReadStatus.id];
[mutableRequest setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if(error){
          DLog(@"Error");
    }
    else{
        DLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }
}];
[uploadTask resume];

I hope I answered your question :)

Happy coding :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM