OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 xmpp 框架中获取消息已读/未读报告? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:53
标题: ios - 如何在 xmpp 框架中获取消息已读/未读报告?

您好,我正在开发聊天应用程序,所以我正在使用 xmpp 框架。聊天工作正常,但如何在 whatsapp、facebook 等中获得消息传递,我搜索了我发现这里的一些文档是我现在的代码已实现

在连接方法中

XMPPMessageDeliveryReceipts* xmppMessageDeliveryRecipts = [[XMPPMessageDeliveryReceipts alloc] initWithDispatchQueue:dispatch_get_main_queue()];
xmppMessageDeliveryRecipts.autoSendMessageDeliveryReceipts = YES;
xmppMessageDeliveryRecipts.autoSendMessageDeliveryRequests = YES;
[xmppMessageDeliveryRecipts activate:self.xmppStream];

在发送消息方法中添加了这一行

NSXMLElement *request = [NSXMLElement elementWithName"request"];
 [request addAttributeWithName"xmlns" stringValue"urn:xmpp:receipts"];
 [message addChild:request];
[message addChild:body];

但是这是用于消息传递与否我们如何检查传递的消息是否已读我已经看到这些扩展 XEP-0184、XEP-0333 但我不知道如何实现已读/未读消息。请帮帮我



Best Answer-推荐答案


如果您想获取已读回执,而不是发送自动消息传递回执,而是在用户阅读该消息时发送它。每条消息都有其对应的 message_id。使用该 message_id 发送已阅读特定消息的送达回执。因此,请评论以下行

//xmppMessageDeliveryRecipts.autoSendMessageDeliveryReceipts = YES;

我通过在消息实体中添加“chatStatus”属性解决了这个问题。对于发件人,我将 chatStatus 的值保持为已发送、未发送或已接收(是否被另一方接收)。对于接收方,我将值保持为已读或未读(我是否已阅读消息,因此对于未读消息,我可以发送已读回执)。

点击发送按钮:

//Save to your Message Entity 

NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
[m setObject: message_body forKey"message_body"];
[m setObject:messageID forKey"message_id"];
[m setObject"yes" forKey"isOutgoing"];
[m setObject:dateString forKey"date"];
[m setObject:timeString forKey"time"];
[m setObject:[NSDate date] forKey"timeStamp"];
[m setObject:yourId forKey:@"from"];
[m setObject:toId forKey:@"to"];

if (!Is_InternetAvailable]) {
 [m setObject:unsent forKey:@"chatStatus"];
}
else{
 [m setObject:sent forKey:@"chatStatus"];
}
[[CoreDataMethods sharedCoreDataMethods] saveUserMessage:m];
}

在 cellForRowAtIndexPath 中:

if ([message isoutGoing]) {//If I have sent the message

        // Mine bubble
        if ([[messageDict valueForKey:@"chatStatus"] isEqualToString:unsent]) {
            //set unsent image
        }
        else if ([[messageDict valueForKey:@"chatStatus"] isEqualToString:sent]){
            //set sent image
        }
        else if ([[messageDict valueForKey:@"chatStatus"] isEqualToString:received]){
          //set Received Image
        }
    }
    else{
        // Other Bubble , Notify them that you have read the message if it is unread/new message

        if ([[messageDict valueForKey:@"chatStatus"] isEqualToString:unread]) {

            //send read receipt
                NSXMLElement *receivedelement = [NSXMLElement elementWithName:@"received" xmlns:@"urn:xmpp:receipts"];

                NSXMLElement *message = [NSXMLElement elementWithName:@"message" xmlns:@"jabber:client"];
                [message addAttributeWithName:@"to" stringValue:toId];
                [message addAttributeWithName:@"from" stringValue:fromID];
                [receivedelement addAttributeWithName:@"id" stringValue:[messageDict valueForKey:@"message_id"]];
                [message addChild:receivedelement];

                //XMPPMessage *generatedReceiptResponse = [[messageDict valueForKey:@"xmppMessage"] generateReceiptResponse];
                [[[kAppDelegate xmppHandler] xmppStream] sendElement:message];

                // update message entity
                [self updateChatStatus:read withMessageID:[messageDict valueForKey:@"message_id"]];
        }
    }

最后当你在didReceiveMessage中收到delivery Receipt时,将chatStatus更新为received

- (void)xmppStreamXMPPStream *)sender didReceiveMessageXMPPMessage *)message{

if ([message hasReceiptResponse]) {//message read
//Update database message entity
 [self updateChatStatus:@"received" withMessageID:[message receiptResponseID]];
}
}

您可以根据需要设置 chatStatus 的值。至于未发送的消息,我已将其设置为在 didSendMessage 委托(delegate)中发送。

希望对你有帮助!!

关于ios - 如何在 xmpp 框架中获取消息已读/未读报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244647/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4