问题
HI I am developing chat app so i am using xmpp framework.Chatting is working fine but how to get the message delivery like in whatsapp, facebook etc.,i searched for that i found some document here is my code upto now i am implemented
in connect Method
XMPPMessageDeliveryReceipts* xmppMessageDeliveryRecipts = [[XMPPMessageDeliveryReceipts alloc] initWithDispatchQueue:dispatch_get_main_queue()];
xmppMessageDeliveryRecipts.autoSendMessageDeliveryReceipts = YES;
xmppMessageDeliveryRecipts.autoSendMessageDeliveryRequests = YES;
[xmppMessageDeliveryRecipts activate:self.xmppStream];
added this lines in sending messsage method
NSXMLElement *request = [NSXMLElement elementWithName:@"request"];
[request addAttributeWithName:@"xmlns" stringValue:@"urn:xmpp:receipts"];
[message addChild:request];
[message addChild:body];
but This is for message delivered or not how can we check the deliverd message read or not i have seen these extentions XEP-0184,XEP-0333 but I don't have any idea to implement the read /unread messages. please help me
回答1:
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. So, comment the following line
//xmppMessageDeliveryRecipts.autoSendMessageDeliveryReceipts = YES;
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.
Hope it Helps!!
回答2:
You can set id attribute of message tag using this id you can check packet is received or not .For more detail read xmpp specification .Check These links
https://xmpp.org/extensions/xep-0359.html
http://xmpp.org/extensions/xep-0184.html
http://xmpp.org/rfcs/rfc3921.html
来源:https://stackoverflow.com/questions/35244647/how-to-get-the-message-read-unread-report-in-xmpp-frame-work