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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…