Retrieving Archived messages from XMPP server in ios
Asked Answered
W

2

8

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?

Wyeth answered 11/1, 2013 at 13:31 Comment(5)
A short update, it seems that the Monitoring plugin added by the Admin does not support retrieving (XEP-0136) of the messages. We added the Open Archive plugin but still nothing.Wyeth
did you get this to work? i'm getting the same error message>Heidt
@Developer007 No, i had to abandon the idea. I think that this is something that just doesn't work. Maybe something has changed over the year but we only have client side archiving at the moment with xmpp.Wyeth
We also are trying to implement something similar. If you do figure it out, remember you are allowed to answer your own question.Bromoform
@Developer007 What kind of db do you have attached to your xmpp server ?Wyeth
P
1

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

Phira answered 21/1, 2021 at 13:46 Comment(0)
D
0

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 [email protected]


    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 = "[email protected]"
                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
    }

}
Dumpy answered 22/11, 2018 at 19:48 Comment(2)
above method is not working if any other method or link to get chat history from Ejabberd serverPhira
@AbishekThangaraj did you find any workable solution to find the chat history between two-person ? if yes kindly help me..Lyso

© 2022 - 2024 — McMap. All rights reserved.