1

I am using quickblox android sdk for a groupchat application and it is working perfectly well except for the fact that i am unable to receive push notification if a message is sent when i was offline. So i decided to query for unread messages count but am getting 0 from the server. I don't know what i have to do to get groupchat dialog for a user to be able to receive notification when the user was not online.

This is the code I used for querying for unread messages:

Set<String> dialogIds = new HashSet<String>();
String groupChatId = groupChat.getDialogId();
System.out.println("GroupChat Id: "+groupChatId);
dialogIds.add(groupChatId);  
QBChatDialog chatDialog = new QBChatDialog(groupChatId);
QBMessageGetBuilder messageGetBuilder = new QBMessageGetBuilder();
messageGetBuilder.setLimit(500);
messageGetBuilder.sortDesc("date_sent");
QBRestChatService.getTotalUnreadMessagesCount(dialogsIds).performAsync(new QBEntityCallback<Integer>() {
@Override
public void onSuccess(Integer total, Bundle params) {
    Log.i(TAG, "total unread messages: " + total);
    // if you have more then one dialog you can get each value with params.getInt(dialog_id)
}

@Override
public void onError(QBResponseException e) { 
    e.printStackTrace();
}
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Collins
  • 145
  • 2
  • 15

1 Answers1

1

The first part is correct:

Set<String> dialogIds = new HashSet<String>();
String groupChatId = groupChat.getDialogId();
dialogsIds.add(groupChatId);

But, after that you were not using dialogIds. And also, instead of getDialogMessages () you need to use getTotalUnreadMessagesCount() as following:

QBRestChatService.getTotalUnreadMessagesCount(dialogIds).performAsync(new QBEntityCallback<Integer>() {
    @Override
    public void onSuccess(Integer total, Bundle params) {
        Log.i(TAG, "totat messages: " + total);
        // if you have more then one dialog you can get each value with params.getInt(dialog_id)
    }

    @Override
    public void onError(QBResponseException e) { }
});
GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • =Am sorry i posted the wrong code, I used `QBRestChatService.getTotalUnreadMessagesCount` and it returned `0` even if i sent a couple of messages to the groupchat using another user's account – Collins Dec 31 '16 at 13:05
  • Edit your question then – GVillani82 Dec 31 '16 at 13:06
  • I have done that, please check if you can help me solve the issue, i have been on it for a week now. – Collins Dec 31 '16 at 13:08
  • It seems correct now. The only reason I see is that you are not logging out the chat maybe, so the counter will always be zero. Try yo logout each time your app goes in background. And login again when you are in foreground – GVillani82 Dec 31 '16 at 13:56
  • What if the user goes offline or the cellular data looses connection, how i won't be able to log out. Do you think i can get your contact, i need someone who can answer questions on quickblox for me. I am new to the api and i am really lost in it. – Collins Dec 31 '16 at 16:00
  • If you can't logout then you can't either request the unread messages – GVillani82 Dec 31 '16 at 18:38