iosobjective-cswiftejabberdxmppframework

Issue on retrieve the XMPP archived message from ejabberd server(Chat history)


I used the following method to retrieve the chat history.

func getArchieveMessages(forUser user:String){
    let xmppMAM = XMPPMessageArchiveManagement.init()
    xmppMAM.addDelegate(self, delegateQueue: .main)
    xmppMAM.activate(stream)
    let xmppDateString = NSDate().addingTimeInterval(-(3 * 24 * 60 * 60)).xmppDateTimeString
    var fields: [XMLElement] = []
    let start = XMPPMessageArchiveManagement.field(withVar: "end", type: nil, andValue: xmppDateString)
    fields.append(start)
     let value = DDXMLElement(name: "value", stringValue: user)
     let child = DDXMLElement(name: "field")
     child.addChild(value)
     child.addAttribute(withName: "var", stringValue: "with")
     let set = XMPPResultSet(max: 20, before: "")
    fields.append(child)
    xmppMAM.retrieveMessageArchive(at: nil, withFields: fields, with: set)
}

After call this function I received the two delegates. i.e. if give 20 messages in XMPPResultSet I received 20 times xmppStreamDidFilterStanza(_ sender: XMPPStream) method.

func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
    print("didFinishReceivingMessagesWith", resultSet)
}
    func xmppStreamDidFilterStanza(_ sender: XMPPStream) {
    debugPrint("xmppStreamDidFilterStanza")
}

Response in func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet)

didFinishReceivingMessagesWith <set xmlns="http://jabber.org/protocol/rsm"><count>364</count><first>1603801030936227</first><last>1603948285226175</last></set>

But this method is never called.

func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
  if let body = message.mamResult?.forwardedMessage{
    print("didReceiveMAMMessage", body)
  }
    print("didReceiveMAMMessage", message)
}

if anyone face this issue and resolved or any known the issue to solve please share your answer.


Solution

  • Below code previously i used for message get delegate. Its totally wrong

    func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
     guard let body = (message.body?.replacingOccurrences(of: "\t", with: String.empty))?.replacingOccurrences(of: "\\s+$", with: String.empty, options: .regularExpression) else {
       return nil
     }
     debugPrint(body)
     return message
    }
    

    Return the message is necessary for get history messages. previously I return nil value in guard let part

    return nil

    func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
     if let forwardedMessage = message.mamResult?.forwardedMessage{
       debugPrint(forwardedMessage)
       return message
     }
     guard let body = (message.body?.replacingOccurrences(of: "\t", with: String.empty))?.replacingOccurrences(of: "\\s+$", with: String.empty, options: .regularExpression) else {
       return nil
     }
     debugPrint(body)
     return message
    }