我在我的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,是两回事吗?
答案 0 :(得分: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 framework 上面的代码可以完美地从服务器获取值。
希望这篇文章对你有用@Akash Thakkar
答案 1 :(得分: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
}
}