8

我正在我的 ios 应用程序中集成 XMPP 功能,但遇到了一个我无法解决的问题。问题是我无法从服务器获取存档消息。我的客户能够登录,并且我已经成功测试了几个服务调用(发送、接收消息、获取有关用户的信息)。

发送时

<iq type='get' id='pref1'>
  <pref xmlns='urn:xmpp:archive'/>
</iq>

回应是

SEND: <iq type="get"><pref xmlns="urn:xmpp:archive"/></iq>

RECV: <iq xmlns="jabber:client" type="error" to="1@iis2/ae76edc"><error code="501"    
type="cancel"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-
stanzas"/</error></iq>

服务器管理员在激活归档时能够看到归档的消息。

为了实现这个功能,必须在服务器端或客户端做些什么吗?难道是看到旧消息和服务器实际实现和支持 XEP-0136,是两件不同的事情吗?

4

2 回答 2

1

如果您想从服务器获取意味着使用此代码

  internal var xmppMAM: XMPPMessageArchiveManagement?

func setupXMPPMam(){
    xmppMAM = XMPPMessageArchiveManagement.init()
    xmppMAM?.addDelegate(self, delegateQueue: .global(qos: .background))
    // stream is XMPPStream
    xmppMAM?.activate(stream)
}

XMPP 连接后调用 setupMam 函数

  func retrieveArchiveMessage(){
    let set = XMPPResultSet(max: totalCount)
    xmppMAM?.retrieveMessageArchive(at: XMPPJID(string: user), withFields: nil, with: set)
  }

 func xmppStream(_ sender: XMPPStream, willReceive message: XMPPMessage) -> XMPPMessage? {
    if let forwardedMessage = message.mamResult?.forwardedMessage{
      debugPrint(forwardedMessage)
      return message
    }
  }

如果您使用Robbiehanson 框架,上面的代码非常适合从服务器获取值。

我希望这篇文章对你有用@Akash Thakkar

于 2021-01-21T13:46:51.217 回答
0

在 Swift 4 中获取归档消息的示例

声明并初始化变量 XMPPMessageArchivingCoreDataStorage 我在其中初始化 XMPPStream

var xmppMessageStorage: XMPPMessageArchivingCoreDataStorage?
var xmppMessageArchiving: XMPPMessageArchiving?

xmppMessageStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
    xmppMessageArchiving = XMPPMessageArchiving(messageArchivingStorage: xmppMessageStorage)

    xmppMessageArchiving?.clientSideMessageArchivingOnly = true
    xmppMessageArchiving?.activate(stream)
    xmppMessageArchiving?.addDelegate(self, delegateQueue: DispatchQueue.main)

这样做,每当消息到达时,这将导致它被归档而无需执行任何其他操作。

然后,检索归档的消息

func RecibedMessageArchiving(idFriend: String) {

    let JabberIDFriend = idFriend   //id friend chat, example test1@example.com


    let moc = xmppMessageStorage?.mainThreadManagedObjectContext
    let entityDescription = NSEntityDescription.entity(forEntityName: "XMPPMessageArchiving_Message_CoreDataObject", in: moc!)
    let request = NSFetchRequest<NSFetchRequestResult>()
    let predicateFormat = "bareJidStr like %@ "
    let predicate = NSPredicate(format: predicateFormat, JabberIDFriend)

    request.predicate = predicate
    request.entity = entityDescription

    //jabberID id del usuario, cliente
    var jabberIDCliente = ""
    if let jabberj = globalChat.value(forKey: "jabberID"){
        jabberIDCliente = jabberj as! String
    }


    do {
        let results = try moc?.fetch(request)

        for message: XMPPMessageArchiving_Message_CoreDataObject? in results as? [XMPPMessageArchiving_Message_CoreDataObject?] ?? [] {

            var element: DDXMLElement!
            do {
                element = try DDXMLElement(xmlString: (message as AnyObject).messageStr)
            } catch _ {
                element = nil
            }

            let body: String
            let sender: String
            let date: NSDate
            let isIncomings: Bool
            if message?.body != nil {
                body = (message?.body)!
            } else {
                body = ""
            }



            if element.attributeStringValue(forName: "to") == JabberIDFriend {
                sender = jabberIDCliente
                isIncomings = false

            } else {
                sender = "test2@example.com"
                isIncomings = true

            }


                var m: [AnyHashable : Any] = [:]
                m["msg"] = message?.body

                print("body", message?.body)

                print("test", element.attributeStringValue(forName: "to"))
                print("test2", element.attributeStringValue(forName: "body"))


        }
    } catch _ {
        //catch fetch error here
    }

}
于 2018-11-22T19:48:04.693 回答