我遇到了一个问题,我在群组类型 QBChatDialogTypePublicGroup 中聊天时没有收到消息。但是,我可以在日志中看到消息正在发送给我。根据quickblox官方网站的解释,收到消息时应调用以下方法
- (void)chatRoomDidReceiveMessageQBChatMessage )message fromDialogIdNSString )dialogId
- (void)chatDidNotSendMessageQBChatMessage )message toDialogIdNSString )dialogId errorNSError *)error
但以上委托(delegate)都没有调用。
这是我采取的步骤 -
登录:-
[QBRequest logInWithUserLogin:[[AppDelegate sharedAppDelegate]getQBUserInstance].login password:[[AppDelegate sharedAppDelegate]getQBUserInstance].password successBlock:^(QBResponse response, QBUUser user) {
NSLog(@"quickblox user id is %lu", (unsigned long)user.ID);
[[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat"%lu", (unsigned long)user.ID]forKey"QuickbloxUserID"];
[[AppDelegate sharedAppDelegate]getQBUserInstance].ID=user.ID;
// set Chat delegate
[[QBChat instance] addDelegate:self];
// login to Chat
[[QBChat instance] loginWithUser:[[AppDelegate sharedAppDelegate]getQBUserInstance]];
} errorBlock:^(QBResponse *response) {
// error handling
NSLog(@"error: %@", response.error);
}];
-(void) chatDidLogin{
[QBChat instance].keepAliveInterval = 30;
[QBChat instance].autoReconnectEnabled = YES;
[QBChat instance].streamManagementEnabled = YES;
NSLog(@"You have successfully signed in to QuickBlox Chat");
[MyNetWorking endHud];
FMTabBarController *vc = [self.storyboard instantiateViewControllerWithIdentifier"tabView"];
[AppDelegate sharedAppDelegate].window.rootViewController=vc;
//[self presentViewController:vc animated:YES completion:nil];
}
- (void)chatDidNotLoginWithErrorNSError *)error{
NSLog(@"You have successfully signed in to QuickBlox Chat %@", error.domain);
NSLog(@"You have successfully signed in to QuickBlox Chat %@", error.userInfo);
NSLog(@"You have successfully signed in to QuickBlox Chat %@", error.localizedDescription);
}
- (void)chatDidConnect{
NSLog(@"You have successfully signed in to QuickBlox Chat");
}
- (void)chatDidAccidentallyDisconnect{
NSLog(@"You have successfully signed in to QuickBlox Chat");
}
- (void)chatDidReconnect{
NSLog(@"You have successfully signed in to QuickBlox Chat");
}
要加入群组,我使用了以下代码
groupChatDialog = [[QBChatDialog alloc] initWithDialogID:self.groupChatID typeBChatDialogTypePublicGroup];
NSLog(@"Chat dialog %@", [QBChat instance].delegates);
// [[QBChat instance] addDelegate:self];
// NSLog(@"Chat dialog %@", [QBChat instance].delegates);
[groupChatDialog setOnJoin:^() {
[[[UIAlertView alloc] initWithTitle"FM" message"Group Joined Successfully" delegate:nil cancelButtonTitle"Ok" otherButtonTitles"Cancel", nil] show];
}];
[groupChatDialog setOnJoinFailed:^(NSError *error) {
[[[UIAlertView alloc] initWithTitle"FM" message:error.localizedDescription delegate:nil cancelButtonTitle"Ok" otherButtonTitles"Cancel", nil] show];
NSLog(@"Error is %@", error);
}];
[groupChatDialog join];
************************************************************
To fetch previous chat I have used the following code
************************************************************
QBResponsePage *resPage = [QBResponsePage responsePageWithLimit:20 skip:0];
[QBRequest messagesWithDialogID:self.groupChatID extendedRequest:nil forPage:resPage successBlock:^(QBResponse response, NSArray messages, QBResponsePage *responcePage) {
NSLog(@"messages are %@", messages);
[arrayChat addObjectsFromArray:messages];
[self.tableView reloadData];
} errorBlock:^(QBResponse *response) {
NSLog(@"error: %@", response.error);
}];
为了发送消息,我使用了以下代码
messageToSent = [QBChatMessage message];
[messageToSent setText:_textField.text];
[messageToSent setDateSent:[NSDate date]];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"save_to_history"] = @YES;
[messageToSent setCustomParameters:params];
[groupChatDialog sendMessage:messageToSent];
_textField.text = @"";
[arrayChat addObject:messageToSent];
[_tableView reloadData];
在此之后,我希望调用以下方法
- (void)chatRoomDidReceiveMessageQBChatMessage )message fromDialogIdNSString )dialogId{
NSLog(@"message is %@", message);
}
- (void)chatDidNotSendMessageQBChatMessage )message toDialogIdNSString )dialogId error:(NSError *)error{
[[[UIAlertView alloc] initWithTitle:@"FM" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil] show];
NSLog(@"Error is %@", error);
}
但是他们没有被调用,但是他们应该按照网站上的解释被调用。请让我知道我在这里缺少什么或我做错了什么。
首先确保你已经设置了 QBChat 代理,在下面设置使用代码
QBChat.instance.addDelegate(self)
如果您的委托(delegate)方法没有调用,请确保您已连接到聊天。如果您未连接到聊天,将不会调用代表。
尝试使用以下代码连接聊天,然后重试。
*Swift 代码
QBChat.instance.connect(withUserID: "user_id",
password: "password",
completion: { [weak self] error in
guard let self = self else { return }
if let error = error
print("Error occured while connecting to chat")
} else {
print("successfully connected to chat.")
self.onCompleteAuth?()
}
})
Note: chat connection can be lost for some reasons, 1. lost connection to internet, 2. App went in background or got suspended. 3. if you're presenting controllers like UIImagePickerController, Camera, UIDocumentPickerController etc.
Read more about connection lost in quickblox
关于ios - Quickblox 聊天室确实收到消息未通话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139412/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |