Reputation: 767
I get all messages sent to me from all users.
When I get the result it is type of XMPPMessage, I don't know how to extract body from this
This problem is related to get archived messages.
func getALLMessagesFromServerWithXML() {
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: "id", stringValue: "getAllMesseges")
if let aQuery = query {
iq?.addChild(aQuery)
}
xmppStream.send(iq!)
}
The result is gotten from this method:
func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
print(message)
}
<message xmlns="jabber:client" to="f.talebi@x/1516292205485357040111042" from="f.talebi@x"><result xmlns="urn:xmpp:mam:2" id="1530957470465122"><forwarded xmlns="urn:xmpp:forward:0"><message xmlns="jabber:client" lang="en" to="[email protected]" from="[email protected]/134788006381643425047394" type="chat"><archived xmlns="urn:xmpp:mam:tmp" by="[email protected]" id="1530957470465122"></archived><stanza-id xmlns="urn:xmpp:sid:0" by="[email protected]" id="1530957470465122"></stanza-id><body>hi 2018-07-07 09:57:49 +0000</body></message><delay xmlns="urn:xmpp:delay" from="x.ir" stamp="2018-07-07T09:57:50.465122Z"></delay></forwarded></result></message>
How can I extract body from this output? for normal messages I can get body by message.body
but for the archived messages I cannot get body with this code.
According to @andesta.erfan answer, I added these codes:
variable:
private var archiving = XMPPMessageArchiveManagement()
in init()
archiving = XMPPMessageArchiveManagement(dispatchQueue: DispatchQueue.main)
archiving?.activate(xmppStream)
archiving?.addDelegate(self, delegateQueue: DispatchQueue.main)
Extension implementation:
extension XMPPHelper: XMPPMessageArchiveManagementDelegate {
func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
print(message.body())
}
}
But xmppMessageArchiveManagement is never called, xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage)
is called in both situation. when it is an archived message or the normal one.
Upvotes: 0
Views: 1643
Reputation: 5451
Swift 4.2
XMPPStreamDelegate
func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
let body = message.body
}
XMPPMessageArchiveManagementDelegate
func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
if let xmppMessage = message.mamResult?.forwardedMessage {
let body = xmppMessage.body
}
}
Upvotes: 0
Reputation: 998
for regular message you should use:
func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) {
print(message.body())
}
for MAM purposes you should implement XmppMessageArchiveManagement and it,s delegate.one of it's delegate method is this:
func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
print(message.body)
}
you can print the archive one with that. be aware that your outgoing packet should be something like this:
`let value = DDXMLElement(name: "value", stringValue: jid)
let child = DDXMLElement(name: "field")
child.addChild(value)
child.addAttribute(withName: "var", stringValue: "with")
let set = XMPPResultSet(max: 1, before: "")
XmppMessageArchiveModule.retrieveMessageArchive(at: nil, withFields: [child], with: set)`
max: 1
tell the MAM that you want only the last Message for specific jid.
after doing all that please please check this answer [service unavailable error in openfire message archive management
Upvotes: 3
Reputation: 15951
Try this code
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
NSString *str = [[message elementForName:@"body"] stringValue];
NSLog(@"%@",str);
}
Upvotes: 1