對於面臨同樣問題的用戶。
如果您希望獲取已讀回執,而不是發送自動消息傳遞收據,則只要用戶閱讀該消息就發送它。每條消息都有相應的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, but we want to send delivery receipts when user has read the message.
//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。在發件人端,您必須爲chatStatus發送一個值,發送,收到,交付。
希望它有助於!
作爲接收者的角色:我在在線用戶列表中,有人向我發送消息,那麼如何識別誰向我發送了消息? –
在消息節中必須有'from'屬性。從屬性值是發件人JID –
我認爲你沒有得到我..我問的接收器和XEP-0184在所有關於發件人功能 –