如何判断聊天中的消息状态(已读/未读)?

发布于 2024-12-04 11:23:10 字数 38 浏览 1 评论 0原文

如何确定消息状态(已读/未读)。聊天是通过XMPP协议实现的。

How to determine the message status (read/unread). Chat is realized with the XMPP protocol.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三生路 2024-12-11 11:23:11

Xmpp 没有已读/未读收据。虽然收到的是 XEP-0184 中实现的东西。

Xmpp does not have a read/unread receipt. While received is something that was implemented in XEP-0184.

木森分化 2024-12-11 11:23:10

XEP-0184:消息传递收据支持在消息已传递时通知发件人。只要您不希望现有客户发送这些收据,您就可以将其用作构建块 - XEP 目前尚未广泛实施。

XEP-0184: Message Delivery Receipts supports notifying senders when their message has been delivered. You might be able to use that as a building block, as long as you don't expect existing clients to send these receipts -- the XEP is not widely-implemented today.

§对你不离不弃 2024-12-11 11:23:10

如果您想获取已读回执,则不要发送自动消息传递回执,而是在用户阅读该消息时发送它。每条消息都有其对应的message_id。使用该 message_id 发送已阅读的特定消息的送达回执。在建立连接时添加以下代码

//message delivery
    XMPPMessageDeliveryReceipts* xmppMessageDeliveryRecipts = [[XMPPMessageDeliveryReceipts alloc] initWithDispatchQueue:dispatch_get_main_queue()];
//don't write this line as it will send auto receipts whenever message will be delivered
    //xmppMessageDeliveryRecipts.autoSendMessageDeliveryReceipts = YES;
    xmppMessageDeliveryRecipts.autoSendMessageDeliveryRequests = YES;
    [xmppMessageDeliveryRecipts activate:self.xmppStream];

我通过在消息实体中添加“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 委托中发送。

注意:在我的应用程序中,我必须只显示已读、已发送和未设置状态,而不是已发送状态。如果您还想显示递送状态,则不要注释 autoSendMessageDeliveryReceipts,并且每当读取消息时,将 IQ 节发送给发件人而不是递送收据,并相应地更改 chatStatus。

这只是基本想法,您可以根据您的要求使用它。

希望有帮助!

If you want to get the read receipts then instead of sending auto message delivery receipts, send it whenever user reads that message. Each message has it's corresponding message_id. Use that message_id to send the delivery receipt for the particular message that has been read. Add following code while making connection

//message delivery
    XMPPMessageDeliveryReceipts* xmppMessageDeliveryRecipts = [[XMPPMessageDeliveryReceipts alloc] initWithDispatchQueue:dispatch_get_main_queue()];
//don't write this line as it will send auto receipts whenever message will be delivered
    //xmppMessageDeliveryRecipts.autoSendMessageDeliveryReceipts = YES;
    xmppMessageDeliveryRecipts.autoSendMessageDeliveryRequests = YES;
    [xmppMessageDeliveryRecipts activate:self.xmppStream];

I solved this problem by adding 'chatStatus' attribute in my message entity. For sender I have kept value of chatStatus as sent, unsent, or received(received by other side or not). For Receiver Side I have kept the Values as read or unread(Have I read message or not, So that for unread message I could send read Receipts).

On Click Of send Button:

//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];
}

In 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"]];
        }
    }

And finally when you receive the delivery Receipt in didReceiveMessage, update the chatStatus to received

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{

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

You could set the values of chatStatus as per your requirement. As for unsent messages I have set it as sent in didSendMessage delegate.

Note: In my app I had to just show the read, sent and unset status, not the delivered status. If you also wanna show delivery status, then don't comment autoSendMessageDeliveryReceipts and whenever messages are read send the IQ stanza to sender instead of delivery receipt and change the chatStatus accordingly.

This is just the basic idea, you could use it as per your requirement.

Hope it Helps!!

初见终念 2024-12-11 11:23:10

我认为您需要使用显示的聊天标记,按照 http://xmpp.org/extensions/xep -0333.html

I think you need to use the Displayed Chat Marker, per http://xmpp.org/extensions/xep-0333.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文