ios - Quickblox 聊天室确实收到消息未通话
<p><p>我遇到了一个问题,我在群组类型 QBChatDialogTypePublicGroup 中聊天时没有收到消息。但是,我可以在日志中看到消息正在发送给我。根据quickblox官方网站的解释,收到消息时应调用以下方法</p>
<pre><code>- (void)chatRoomDidReceiveMessage:(QBChatMessage )message fromDialogId:(NSString )dialogId
- (void)chatDidNotSendMessage:(QBChatMessage )message toDialogId:(NSString )dialogId error:(NSError *)error
</code></pre>
<p>但以上委托(delegate)都没有调用。</p>
<p>这是我采取的步骤 -</p>
<hr/>
<p>登录:-</p>
<hr/>
<pre><code>getQBUserInstance].login password:[getQBUserInstance].password successBlock:^(QBResponse response, QBUUser user) {
NSLog(@"quickblox user id is %lu", (unsigned long)user.ID);
[ setValue:forKey:@"QuickbloxUserID"];
[getQBUserInstance].ID=user.ID;
// set Chat delegate
[ addDelegate:self];
// login to Chat
[ loginWithUser:[getQBUserInstance]];
} errorBlock:^(QBResponse *response) {
// error handling
NSLog(@"error: %@", response.error);
}];
-(void) chatDidLogin{
.keepAliveInterval = 30;
.autoReconnectEnabled = YES;
.streamManagementEnabled = YES;
NSLog(@"You have successfully signed in to QuickBlox Chat");
;
FMTabBarController *vc = ;
.window.rootViewController=vc;
//;
}
- (void)chatDidNotLoginWithError:(NSError *)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");
}
</code></pre>
<hr/>
<p>要加入群组,我使用了以下代码</p>
<hr/>
<pre><code>groupChatDialog = [ initWithDialogID:self.groupChatID type:QBChatDialogTypePublicGroup];
NSLog(@"Chat dialog %@", .delegates);
// [ addDelegate:self];
// NSLog(@"Chat dialog %@", .delegates);
[groupChatDialog setOnJoin:^() {
[[ initWithTitle:@"FM" message:@"Group Joined Successfully" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil] show];
}];
[groupChatDialog setOnJoinFailed:^(NSError *error) {
[[ initWithTitle:@"FM" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil] show];
NSLog(@"Error is %@", error);
}];
;
************************************************************
To fetch previous chat I have used the following code
************************************************************
QBResponsePage *resPage = ;
[QBRequest messagesWithDialogID:self.groupChatID extendedRequest:nil forPage:resPage successBlock:^(QBResponse response, NSArray messages, QBResponsePage *responcePage) {
NSLog(@"messages are %@", messages);
;
;
} errorBlock:^(QBResponse *response) {
NSLog(@"error: %@", response.error);
}];
</code></pre>
<hr/>
<p>为了发送消息,我使用了以下代码</p>
<hr/>
<pre><code>messageToSent = ;
;
];
NSMutableDictionary *params = ;
params[@"save_to_history"] = @YES;
;
;
_textField.text = @"";
;
;
</code></pre>
<hr/>
<p>在此之后,我希望调用以下方法</p>
<hr/>
<pre><code>- (void)chatRoomDidReceiveMessage:(QBChatMessage )message fromDialogId:(NSString )dialogId{
NSLog(@"message is %@", message);
}
- (void)chatDidNotSendMessage:(QBChatMessage )message toDialogId:(NSString )dialogId error:(NSError *)error{
[[ initWithTitle:@"FM" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil] show];
NSLog(@"Error is %@", error);
}
</code></pre>
<hr/>
<p>但是他们没有被调用,但是他们应该按照网站上的解释被调用。请让我知道我在这里缺少什么或我做错了什么。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先确保你已经设置了 QBChat 代理,在下面设置使用代码</p>
<pre><code>QBChat.instance.addDelegate(self)
</code></pre>
<p>如果您的委托(delegate)方法没有调用,请确保您已连接到聊天。如果您未连接到聊天,将不会调用代表。</p>
<p>尝试使用以下代码连接聊天,然后重试。</p>
<p>*Swift 代码</p>
<pre><code>QBChat.instance.connect(withUserID: "user_id",
password: "password",
completion: { 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?()
}
})
</code></pre>
<blockquote>
<p>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.</p>
</blockquote>
<p> <a href="https://help.quickblox.com/article/372-why-do-i-receive-the-error-socket-closed-by-remote-peer" rel="noreferrer noopener nofollow">Read more about connection lost in quickblox</a> </p></p>
<p style="font-size: 20px;">关于ios - Quickblox 聊天室确实收到消息未通话,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33139412/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33139412/
</a>
</p>
页:
[1]