2016-10-19 71 views
1

我正在使用QuickBlox-iOS SDK進行聊天。登錄/註冊工作完美。另外,我能發送消息,但委託方法chatDidReceiveMessage方法不叫QuickBlox

- (void)chatDidReceiveMessage:(QBChatMessage *)message;

是沒有得到調用。這是我用來設置聊天的代碼。添加以下代碼的appDelegate:

// connect to Chat 
    [[QBChat instance] addDelegate:self]; 

    QBUUser *currentUser = [QBUUser user]; 
    currentUser.ID = [Global sharedInstance].currentUser.ID; 
    currentUser.password = @"password"; 
    [[QBChat instance] connectWithUser:currentUser completion:^(NSError * _Nullable error) { 
     NSLog(@"connect to chat error %@",error); 
    }]; 

而下面的代碼我用來發送消息:

QBChatMessage *message = [QBChatMessage message]; 
message.recipientID=[Global sharedInstance].QBUserID; 
      message.senderID=[Global sharedInstance].currentUser.ID; 
      [message setText:messageTextView.text]; 

      message.dateSent = [NSDate date]; 
      NSMutableDictionary *params = [NSMutableDictionary dictionary]; 
      params[@"save_to_history"] = @YES; 
      [message setCustomParameters:params]; 
      [QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) { 
       NSLog(@"success: %@", createdMessage); 
      } errorBlock:^(QBResponse *response) { 
       NSLog(@"ERROR: %@", response.error); 
      }] 

我對QuickBlox儀表板檢查。它顯示所有發送/接收的消息。但是,當我向另一個用戶發送消息時,代理不會被調用。我沒有使用任何額外的服務類(QMServices),就像他們在其示例項目中使用的那樣。任何幫助,將不勝感激。謝謝

回答

3

我不明白你爲什麼使用[QBRequest createMessage:successBlock:errorBlock:]方法發送消息給其他用戶。

什麼爲我總是工作是創建與你試圖消息的用戶一個chatDialog,像這樣:然後

QBChatDialog *dialog = [[QBChatDialog alloc] initWithDialogID:nil 
                 type: QBChatDialogTypePrivate]; 
dialog.occupantIDs = @[@([Global instance].QBUserID), 
         @([Global instance].currentUser.user.ID)]; 

,你可以調用Quickblox方法在服務器上創建對話框:

if (dialog.ID == nil) { 
    [QBRequest createDialog:dialog successBlock:^(QBResponse *response, QBChatDialog *createdDialog) { 

     [self sendMessageToDialog: dialog withText:@"Hello friend!"]; 

    } errorBlock:^(QBResponse *response) { 
     NSLog(@"dialog creation err: %@", response); 
    }]; 
} 

創建消息:

- (QBChatMessage *) createMessageWithText: (NSString *)text andDialog: (QBChatDialog*)dialog { 
    QBChatMessage *message = [QBChatMessage message]; 
    message.text = text; 
    message.senderID = [Global instance].currentUser.ID; 
    message.markable = YES; 
    message.deliveredIDs = @[@([Global instance].currentUser.ID)]; 
    message.readIDs = @[@([Global instance].currentUser.ID)]; 
    message.dialogID = dialog.ID; 
    message.dateSent = [NSDate date]; 
    message.recipientID = dialog.recipientID; 
    message.customParameters = [NSMutableDictionary dictionary]; 

    message.customParameters[kQMCustomParameterDialogID] = dialog.ID; 
    message.customParameters[kQMCustomParameterDialogType] = [NSString stringWithFormat:@"%lu",(unsigned long)dialog.type]; 
    message.customParameters[@"application_id"] = @"<your-application-id>"; 
    message.customParameters[@"save_to_history"] = @"1"; 

    if (dialog.lastMessageDate != nil){ 
     NSNumber *lastMessageDate = @((NSUInteger)[dialog.lastMessageDate timeIntervalSince1970]); 
     message.customParameters[kQMCustomParameterDialogRoomLastMessageDate] = [lastMessageDate stringValue]; 
    } 
    if (dialog.updatedAt != nil) { 
     NSNumber *updatedAt = @((NSUInteger)[dialog.updatedAt timeIntervalSince1970]); 
     message.customParameters[kQMCustomParameterDialogRoomUpdatedDate] = [updatedAt stringValue]; 
    } 

    return message; 
} 

,然後發送消息對話框:

- (void) sendMessageToDialog: (QBChatDialog *)dialog withText: (NSString *)text { 

    QBChatMessage *message = [[ChatService shared] createMessageWithText:text andDialog:self.dialog]; 

    [dialog sendMessage:message completionBlock:^(NSError * _Nullable error) { 
     if (error != nil) { 
      NSLog(@"error creating message %@", error); 
     } else { 
      NSLog(@"message sent!"); 
     } 
    }]; 
} 

我認爲跟隨這個流量,你將能夠通過委託接收回調。

編輯 - 我忘了說我在代碼中使用以上是consts:

NSString const *kQMCustomParameterDialogID = @"dialog_id"; 
NSString const *kQMCustomParameterDialogRoomName = @"room_name"; 
NSString const *kQMCustomParameterDialogRoomPhoto = @"room_photo"; 
NSString const *kQMCustomParameterDialogRoomLastMessageDate = @"room_last_message_date"; 
NSString const *kQMCustomParameterDialogUpdatedDate = @"dialog_updated_date"; 
NSString const *kQMCustomParameterDialogType = @"type"; 
NSString const *kQMCustomParameterDialogRoomUpdatedDate = @"room_updated_date"; 
+0

感謝您的回答。這些常量是什麼'kQMCustomParameterDialogID','kQMCustomParameterDialogRoomUpdatedDate','kQMCustomParameterDialogRoomLastMessageDate','kQMCustomParameterDialogType'? –

+0

謝謝。代表現在正在接聽電話。但消息未保存在儀表板的歷史記錄中。它出現在最後一條消息中。但是當點擊查看歷史時。消息沒有顯示在那裏。我不想在我的應用程序中使用核心數據。 –

+0

@SushilSharma控制歷史的變量是自定義參數'save_to_history',在這個例子中,我把它放在那裏。你是否擦除了任何自定義參數? – gaskbr

0

您是否已將<QBChatDelegate>添加到.h文件中。

+0

是的,我在實現類增加了它。 –

+0

兄弟,檢查此鏈接:http://stackoverflow.com/questions/33697942/voidchatdidreceivemessageqbchatmessage-message-not-working –

+0

感謝您的答覆。我嘗試了答案中給出的方法。但它不起作用。使用'sendMessage'方法,我甚至無法發送消息。 –