如何在xmpp框架中获取消息读/未读报告?

我正在开发聊天应用程序,所以我使用xmpp framework.Chatting工作正常,但如何获取消息传递,如在WhatsApp,Facebook等,我搜索我发现这里的一些文件是我的代码到现在我实施

在connect方法中

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但我不知道实现读/未读消息。 请帮帮我

如果您想获取已读回执,而不是发送自动邮件传递收据,请在用户阅读该邮件时发送。 每条消息都有相应的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中收到交货收据时,请将chatStatus更新为已收到

 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{ if ([message hasReceiptResponse]) {//message read //Update database message entity [self updateChatStatus:@"received" withMessageID:[message receiptResponseID]]; } } 

您可以根据您的要求设置chatStatus的值。 对于未发送的消息,我将其设置为在didSendMessage委托中发送。

希望能帮助到你!!

您可以使用此ID设置消息标记的id属性,您可以检查是否收到数据包。有关详细信息,请阅读xmpp规范。检查这些链接

 https://xmpp.org/extensions/xep-0359.html http://xmpp.org/extensions/xep-0184.html http://xmpp.org/rfcs/rfc3921.html