Retrieving Archived messages from XMPP server in ios

自作多情 提交于 2019-12-05 21:04:11

问题


I am integrating XMPP functionality in my ios app and i came across a problem i cannot solve. The problem is i cannot get archived messages from the server. My client is able to log in and i have tested several service calls (send, receive messages, getting info about a user) with success.

Upon sending

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

The response is

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>

The server administrator is able to see the archived messages, as he activated archiving.

Must something be done server or client side in order to achieve this functionality? Could it be that seeing old messages and the server actually implementing and supporting XEP-0136, are two different things?


回答1:


an example to get archived messages in Swift 4

declares and initializes the variables XMPPMessageArchivingCoreDataStorage where I initialize the 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)

doing this, whenever a message arrives, this will cause it to be archived without needing to do anything else.

then, to retrieve the archived message

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
    }

}


来源:https://stackoverflow.com/questions/14279249/retrieving-archived-messages-from-xmpp-server-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!