Mike C
Mike C

Reputation: 1

After sending a message QuickBlox, it returns all the messages that has been previously sent

Looking to get some help implementing QuickBlox into our iOS app. We can’t seem to get around an issue that after sending a message QuickBlox returns all the messages that has been previously sent when the block is called by the timer ticks event. Any help would be much appreciated!

    QBResponsePage *page = [QBResponsePage responsePageWithLimit:10 skip:0];

    [QBRequest dialogsForPage:page extendedRequest:nil successBlock:^(QBResponse *response, NSArray *dialogObjects, NSSet *dialogsUsersIDs, QBResponsePage *page)
    {
        QBChatDialog *dialog=[dialogObjects objectAtIndex:0];
        [QBRequest messagesWithDialogID:dialog.ID extendedRequest:nil forPage:page successBlock:^(QBResponse *response, NSArray *messages, QBResponsePage *responsePage)
         {
             [self.items removeAllObjects];
             [self.items addObjectsFromArray:messages];
             [self finishSendingMessageAnimated:YES];

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


    } errorBlock:^(QBResponse *response) {

    }];

Here are the steps we have taken: 1) Imported all of the QuickBlox Classes into our project.

2) In My App Delegate Class set all credentials of QuickBlox

[QBApplication sharedApplication].applicationId = (removed);
[QBConnection registerServiceKey:@“(removed)”];
[QBConnection registerServiceSecret:@“(removed)”];
[QBSettings setAccountKey:@“(removed)”];

3) Then created the subClass of QMChatViewController that was imported

4) Imported NMPaginatorDelegate and Cretae UsersPaginator object, (gives me list of users)

5) On Clicking on Particular user invokes the subClass we created and on its viewDidLoad wrote below code:

QBUUser *currentUser = [QBUUser user];
currentUser.ID = LoginUser.ID;
currentUser.password = LoginUser.login;

[[QBChat instance] addDelegate:(id)self];
[[QBChat instance] loginWithUser:currentUser];

After loginWithUser called

    BOOL chlLogion =[[QBChat instance]isLoggedIn];
    NSLog(@"Loggin IN Status :- %hhd”,chlLogion);

Log Print :- 2015-09-22 16:04:31.911 AppName[2263:96572] [ChatService] Connecting to Chat, host: chat.quickblox.com, user JID: [email protected]/7DE2CD1E-481F-4922-B21D-8EB14BF8B55F 2015-09-22 16:04:42.978 AppName[2263:96225] Loggin IN Status :- 0

Note :- Deprication

 [[QBChat instance] sendMessage:message];

That Code is Depricated and and your sdks shows it option but that well also not working.

and set its delegate Methods.

However, it just called 1 delegate “chatDidConnect” after that NO another delegate is called. And in Send Message I am setting:

    QBChatDialog *chatDialog = [[QBChatDialog alloc]initWithDialogID:[NSString stringWithFormat:@"%lu",(unsigned long)LoginUser.ID] type:QBChatDialogTypePrivate];
    chatDialog.occupantIDs=@[@(SelectedChatUser.ID)];
    chatDialog.name = @"school friends";

    [QBRequest createDialog:chatDialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) {

        _dialog=createdDialog;
        QBChatMessage *message = [QBChatMessage message];

        message.text = text;
        message.senderID = senderId;
        message.recipientID=createdDialog.recipientID;
        message.deliveredIDs=[createdDialog.occupantIDs objectAtIndex:1];
        message.dialogID=[NSString stringWithFormat:@"%@",createdDialog.ID];
        message.senderNick=@"Nick Simulator";
        message.dateSent = [NSDate date];

        [self.items addObject:message];

        [createdDialog sendMessage:message];
        [self finishSendingMessageAnimated:YES];

    } errorBlock:^(QBResponse *response) {

    }];

But threw that code messages are not sent and we don’t get any messages threw delegate.

6) After that I switched to new methods to send a message. Wrote the following code to send a message:

    - (void)didPressSendButton:(UIButton *)button
       withMessageText:(NSString *)text
              senderId:(NSUInteger)senderId
     senderDisplayName:(NSString *)senderDisplayName
                  date:(NSDate *)date {

QBChatDialog *chatDialog = [[QBChatDialog alloc]initWithDialogID:[NSString stringWithFormat:@"%lu",(unsigned long)LoginUser.ID] type:QBChatDialogTypePrivate];
    chatDialog.occupantIDs=@[@(SelectedChatUser.ID)];
    chatDialog.name = @"school friends";
         [QBRequest createDialog:chatDialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) {

        _dialog=createdDialog;
        QBChatMessage *message = [QBChatMessage message];

        message.text = text;
        message.senderID = senderId;
        message.recipientID=createdDialog.recipientID;
        message.deliveredIDs=[createdDialog.occupantIDs objectAtIndex:1];
        message.dialogID=[NSString stringWithFormat:@"%@",createdDialog.ID];
        message.senderNick=@"Nick Simulator";
        message.dateSent = [NSDate date];

        [self.items addObject:message];

        [QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) {
            NSLog(@"success: %@", createdMessage);
        } errorBlock:^(QBResponse *response) {
            NSLog(@"ERROR: %@", response.error);
        }];
     [self finishSendingMessageAnimated:YES];

      } errorBlock:^(QBResponse *response) {

     }];

Upvotes: 0

Views: 1331

Answers (1)

Manoj
Manoj

Reputation: 1069

The dialogsForpage of QBResponsePage will return all the previous messages. So skip the message count that you already have will return you only latest messages.

let page = QBResponsePage.(limit:10, skip: message count that you already have)
let parameters = ["sort_desc" : "date_sent"]
QBRequest.messagesWithDialogID(currentDialog.ID!, 
                               extendedRequest: parameters,
                               forPage: resPage, 
                               successBlock:{ (response: QBResponse, messages: [QBChatMessage]?, page: QBResponsePage?) -> Void in
                               /**your code**/
                               }

Upvotes: 2

Related Questions