- (void)chatDidReceiveMessage :( QBChatMessage *)消息不起作用

时间:2015-11-13 16:56:59

标签: ios objective-c chat quickblox xmppframework

我已经整合了QuickBlox iOS sdk v2.5。我正在向特定消息发送消息,然后将它们发送到服务器但是用于接收消息   - (void)chatDidReceiveMessage :( QBChatMessage *)消息未被调用

这就是我要做的连接

[[QBChat instance] addDelegate:self];

QBUser *chatUser=[QBUser new];
chatUser.ID=[[[NSUserDefaults standardUserDefaults] objectForKey:USERID] integerValue];
chatUser.password=[[NSUserDefaults standardUserDefaults] objectForKey:PASSWORD];
[[QBChat instance] connectWithUser:chatUser completion:nil];

用于创建chatDialog

QBChatDialog * chatDialog = [[QBChatDialog alloc] initWithDialogID:NULL类型:QBChatDialogTypePrivate];

chatDialog.name = @"Chat with Garry";

NSMutableArray *chatPartners=[[NSMutableArray alloc] initWithObjects:[chatPartner objectForKey:@"id"] ,[[NSUserDefaults standardUserDefaults] objectForKey:USERID], nil];

chatDialog.occupantIDs=chatPartners;

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

    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:response.data options:kNilOptions error:nil];

    [[NSUserDefaults standardUserDefaults] setObject:[json object
} errorBlock:^(QBResponse *response) {

 //error

}];

然后发送消息

QBChatMessage *message =[QBChatMessage message];
[message setText:self.messageText.text];

params[@"messageStatus"]=@"Test Message";
params[@"save_to_history"] = @YES;

[message setCustomParameters:params];

[message setRecipientID:[[[NSUserDefaults standardUserDefaults] objectForKey:PARTNERID] integerValue]]; //



[QBRequest createMessage:message successBlock:^(QBResponse *response, QBChatMessage *createdMessage) {

    self.messageText.text = @"Type Here...";
    [self addMessagetoChat:createdMessage];


    NSLog(@"success: %@", createdMessage);
} errorBlock:^(QBResponse *response) {
    self.messageText.text = @"Type Here...";
    NSLog(@"ERROR: %@", response.error);
}];

消息被发送到聊天但是其他用户无法接收它.-(void)chatDidReceiveMessage :( QBChatMessage *)消息未被调用。或者我可能正在使用错误的功能。

1 个答案:

答案 0 :(得分:2)

您不是在发送消息,而是在REST中创建消息。所以QBChat委托' chatDidReceiveMessage'不会被叫。这仍然是有效的事情,但要接收此类消息,您需要从REST下载它(例如' [QBRequest messagesForDialogID:completionBlock:errorBlock:]')。

要通过委托接收消息,您需要使用' sendMessage:completion:' QBChatDialog,它使用XMPP发送和接收消息。 在你的情况下,你应该这样做:

QBChatMessage *message =[QBChatMessage message];
[message setText:self.messageText.text];

params[@"messageStatus"]=@"Test Message";
params[@"save_to_history"] = @YES;

[message setCustomParameters:params];

[message setRecipientID:[[[NSUserDefaults standardUserDefaults] objectForKey:PARTNERID] integerValue]];

[chatDialog sendMessage:message completionBlock:^(NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Failed to send message with error: %@", error);
    }
}];