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

时间:2011-09-13 12:41:06

标签: xcode xmpp chat message status

如何确定消息状态(已读/未读)。使用XMPP协议实现聊天。

4 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:2)

我认为您需要按照http://xmpp.org/extensions/xep-0333.html

使用显示的聊天标记

答案 2 :(得分:2)

如果您想获取已读回执而不是发送自动留言递送收据,请在用户阅读该留言时发送。每条消息都有相应的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的值保持为已发送,未发送或已接收(由其他方接收或未接收)。对于Receiver Side,我将值保持为已读或未读(我是否已阅读消息,因此对于未读消息,我可以发送已读回执)。

点击发送按钮:

//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。

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

希望它能帮助!!

答案 3 :(得分:0)

Xmpp没有已读/未读回执。收到的东西是在XEP-0184中实现的。