在iOS中从XMPP服务器检索已归档消息 [英] Retrieving Archived messages from XMPP server in ios

查看:98
本文介绍了在iOS中从XMPP服务器检索已归档消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将XMPP功能集成到我的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.

发送时

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

服务器管理员在激活归档后能够看到已归档的消息.

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

为了实现此功能,必须在服务器或客户端进行一些操作吗?可能是看到旧消息和服务器实际实现并支持XEP-0136是两个不同的事情吗?

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?

推荐答案

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

an example to get archived messages in Swift 4

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

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
    }

}

这篇关于在iOS中从XMPP服务器检索已归档消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆