简体   繁体   中英

Retrieving Archived messages from XMPP server in ios

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?

If you want fetch from server means use this code

  internal var xmppMAM: XMPPMessageArchiveManagement?

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

call the setupMam function once XMPP connect

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

if you using Robbiehanson framework above code is working perfectly for fetch value from server.

I hope this article is useful for you @Akash Thakkar

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
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM