Reputation: 767
I want to retrieve the last message I sent to someone in XMPP.
I have written this code, but it fetches all sent messages:
let query = try? XMLElement(xmlString: "<query xmlns='urn:xmpp:mam:2'/>")
let iq = XMLElement.element(withName: "iq") as? XMLElement
iq?.addAttribute(withName: "type", stringValue: "set")
iq?.addAttribute(withName: "from", stringValue: "[email protected]")
iq?.addAttribute(withName: "max", stringValue: "1")
iq?.addAttribute(withName: "id", stringValue: "GetLastUserMessage")
if let aQuery = query {
iq?.addChild(aQuery)
}
xmppStream.send(iq!)
Upvotes: 0
Views: 667
Reputation: 998
first you should have xmppMessageArchiveManagement module:
var xmppMAM: XMPPMessageArchiveManagement!
then active it
xmppMAM.activate(xmppStream)
after that you can use protocol function of xmppMessageArchiveManagementDelegate
extension someClass: XMPPMessageArchiveManagementDelegate {
func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
}
func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didFinishReceivingMessagesWith resultSet: XMPPResultSet) {
}
}
now you can create packet and send them and two above function will catch the packet of server : caution:you should use mam:1 instead of mam:2 and use messageArchiveManagement for all retrieving tasks.example:for retrieving your last message build this packet and use mam for sending it.
`let value = DDXMLElement(name: "value", stringValue: youJid)
let child = DDXMLElement(name: "field")
child.addChild(value)
child.addAttribute(withName: "var", stringValue: "with")
let set = XMPPResultSet(max: 1, before: "")
xmppMam.retrieveMessageArchive(at: nil, withFields: [child], with: set)`
Upvotes: 2