如何从XMPPMessage提取接收消息正文?

时间:2018-07-07 11:39:55

标签: swift xmppframework

我收到了所有用户发送给我的所有消息。
当我得到的结果是XMPPMessage类型时,我不知道如何从中提取正文

  

此问题与获取存档的消息有关。

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!)
}

结果是通过这种方法获得的:

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="a.mardani@xmpp.x.ir" from="f.talebi@x.ir/134788006381643425047394" type="chat"><archived xmlns="urn:xmpp:mam:tmp" by="f.talebi@x.ir" id="1530957470465122"></archived><stanza-id xmlns="urn:xmpp:sid:0" by="f.talebi@x.ir" 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>

如何从此输出中提取主体?对于普通邮件,我可以通过message.body来获取正文,但对于存档邮件,则无法使用此代码获得正文。

根据@ andesta.erfan的答案,我添加了以下代码:

变量:

private var archiving = XMPPMessageArchiveManagement()

在init()

archiving = XMPPMessageArchiveManagement(dispatchQueue: DispatchQueue.main)
archiving?.activate(xmppStream)    
archiving?.addDelegate(self, delegateQueue: DispatchQueue.main)

扩展实施:

extension XMPPHelper: XMPPMessageArchiveManagementDelegate {

    func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) {
        print(message.body())
    }

}

但是从不调用xmppMessageArchiveManagement,在两种情况下都调用xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage)。是已存档的邮件还是普通邮件。

3 个答案:

答案 0 :(得分:3)

对于常规消息,您应该使用:

func xmppStream(_ sender: XMPPStream, didReceive message: XMPPMessage) { print(message.body()) }

出于MAM目的,您应该实现XmppMessageArchiveManagement及其委托。它的委托方法之一是:

func xmppMessageArchiveManagement(_ xmppMessageArchiveManagement: XMPPMessageArchiveManagement, didReceiveMAMMessage message: XMPPMessage) { print(message.body) }

您可以使用它来打印存档。 请注意,您的外发数据包应该是这样的:

`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告诉MAM您只需要最后一个消息来发送特定的jid。 完成所有操作后,请检查此答案[service unavailable error in openfire message archive management

答案 1 :(得分:1)

尝试使用此代码

 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
    {
        NSString *str = [[message elementForName:@"body"] stringValue];
        NSLog(@"%@",str);
    }

答案 2 :(得分:0)

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
  }
}