2013-12-12 58 views
4

我使用xmppframework開發聊天應用XMPP架構未讀,我很順利地完成了發送和接收消息,但如何檢查收到的消息是讀還是在IOS

1)如果我與用戶聊天,並用戶B向我發送消息,我如何識別用戶B向我發送消息。 (我已經保持在覈心數據的消息,以便消息將被保存) 2)如何能夠識別接收消息已讀或未讀

注:消息保存我用

xmppMessageArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance]; 
xmppMessageArchivingModule = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageArchivingStorage]; 

感謝

回答

4

你可以看看XEP-0184

這應該是消息節格式

<message 
    from='[email protected]/resource' 
    id='messageId-1010' 
    to='[email protected]/resource'> 
    <body>your message </body> 
    <request xmlns='urn:xmpp:receipts'/> 
</message> 

,這是收到的響應消息

<message 
    from='[email protected]/resource' 
    id='packetId' 
    to='[email protected]/resource'> 
    <received xmlns='urn:xmpp:receipts' id='messageId-1010'/> 
</message> 

這裏messageId-1010將是獨一無二的,讓你知道它是哪個消息響應。

+0

作爲接收者的角色:我在在線用戶列表中,有人向我發送消息,那麼如何識別誰向我發送了消息? –

+0

在消息節中必須有'from'屬性。從屬性值是發件人JID –

+0

我認爲你沒有得到我..我問的接收器和XEP-0184在所有關於發件人功能 –

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, 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發送一個值,發送,收到,交付

希望它有助於!

+0

在您的代碼什麼是「收到「和」閱讀「? – Bittoo

+0

可以請你幫我解決如何檢測消息是未讀還是讀取? – Bucket

相關問題