菜鸟教程小白 发表于 2022-12-13 11:20:28

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(@&#34;quickblox user id is %lu&#34;, (unsigned long)user.ID);
    [ setValue:forKey:@&#34;QuickbloxUserID&#34;];

    [getQBUserInstance].ID=user.ID;

    // set Chat delegate
    [ addDelegate:self];

    // login to Chat
    [ loginWithUser:[getQBUserInstance]];


} errorBlock:^(QBResponse *response) {

    // error handling
    NSLog(@&#34;error: %@&#34;, response.error);
}];


-(void) chatDidLogin{

    .keepAliveInterval = 30;
    .autoReconnectEnabled = YES;
    .streamManagementEnabled = YES;

    NSLog(@&#34;You have successfully signed in to QuickBlox Chat&#34;);

    ;
    FMTabBarController *vc = ;
    .window.rootViewController=vc;

    //;
}


- (void)chatDidNotLoginWithError:(NSError *)error{
    NSLog(@&#34;You have successfully signed in to QuickBlox Chat %@&#34;, error.domain);
    NSLog(@&#34;You have successfully signed in to QuickBlox Chat %@&#34;, error.userInfo);
    NSLog(@&#34;You have successfully signed in to QuickBlox Chat %@&#34;, error.localizedDescription);
}


- (void)chatDidConnect{
    NSLog(@&#34;You have successfully signed in to QuickBlox Chat&#34;);
}


- (void)chatDidAccidentallyDisconnect{
    NSLog(@&#34;You have successfully signed in to QuickBlox Chat&#34;);
}


- (void)chatDidReconnect{
    NSLog(@&#34;You have successfully signed in to QuickBlox Chat&#34;);
}
</code></pre>

<hr/>

<p>要加入群组,我使用了以下代码</p>

<hr/>

<pre><code>groupChatDialog = [ initWithDialogID:self.groupChatID type:QBChatDialogTypePublicGroup];
NSLog(@&#34;Chat dialog %@&#34;, .delegates);

// [ addDelegate:self];
// NSLog(@&#34;Chat dialog %@&#34;, .delegates);

[groupChatDialog setOnJoin:^() {
    [[ initWithTitle:@&#34;FM&#34; message:@&#34;Group Joined Successfully&#34; delegate:nil cancelButtonTitle:@&#34;Ok&#34; otherButtonTitles:@&#34;Cancel&#34;, nil] show];
}];

[groupChatDialog setOnJoinFailed:^(NSError *error) {
    [[ initWithTitle:@&#34;FM&#34; message:error.localizedDescription delegate:nil cancelButtonTitle:@&#34;Ok&#34; otherButtonTitles:@&#34;Cancel&#34;, nil] show];
    NSLog(@&#34;Error is %@&#34;, 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(@&#34;messages are %@&#34;, messages);
    ;
    ;

} errorBlock:^(QBResponse *response) {
    NSLog(@&#34;error: %@&#34;, response.error);
}];
</code></pre>

<hr/>

<p>为了发送消息,我使用了以下代码</p>

<hr/>

<pre><code>messageToSent = ;

;
];

NSMutableDictionary *params = ;
params[@&#34;save_to_history&#34;] = @YES;
;
;

_textField.text = @&#34;&#34;;
;
;
</code></pre>

<hr/>

<p>在此之后,我希望调用以下方法</p>

<hr/>

<pre><code>- (void)chatRoomDidReceiveMessage:(QBChatMessage )message fromDialogId:(NSString )dialogId{
    NSLog(@&#34;message is %@&#34;, message);
}

- (void)chatDidNotSendMessage:(QBChatMessage )message toDialogId:(NSString )dialogId error:(NSError *)error{
    [[ initWithTitle:@&#34;FM&#34; message:error.localizedDescription delegate:nil cancelButtonTitle:@&#34;Ok&#34; otherButtonTitles:@&#34;Cancel&#34;, nil] show];
    NSLog(@&#34;Error is %@&#34;, 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: &#34;user_id&#34;,
                            password: &#34;password&#34;,
                            completion: { error in
      guard let self = self else { return }
      if let error = error
            print(&#34;Error occured while connecting to chat&#34;)
      } else {
            print(&#34;successfully connected to chat.&#34;)
            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&#39;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]
查看完整版本: ios - Quickblox 聊天室确实收到消息未通话