情景:
问题:
保存所有聊天记录并检索它们已经实施。
我正在使用XMPPMessageArchiving
& XMPPMessageArchivingCoreDataStorage
实现同样的目标。
如果有人能指出我正确的方向来解决这个问题,那将会非常有帮助。
答案 0 :(得分:7)
离线消息是服务器的责任。 XEP- 160
中介绍了如何处理离线消息在客户端,您要确保发送的消息节的类型为" normal"或者"聊天"。
您还要确保在eJabberd中启用了离线消息存储。
上线后,您可以逐个获取所有离线消息
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message`
XMPPMessage将如下所示,带有延迟和时间戳标记。
<message xmlns="jabber:client" from="demo@demoHost/demoResource" to="you@demoHost" type="chat" id="234E5B61-C450-4F15-B2E6-5AF68AA8D376"><body>offline message test body</body><html xmlns="http://jabber.org/protocol/xhtml-im"><body xmlns="http://www.w3.org/1999/xhtml" style="background-color:#e9e9e9;color:#000000;"><span style="font-family: 'Helvetica';font-size: 12px;">offline message test body</span></body></html><x xmlns="jabber:x:event"><composing></composing></x><active xmlns="http://jabber.org/protocol/chatstates"></active><delay xmlns="urn:xmpp:delay" from="suhailk-imac.local" stamp="2014-09-19T10:41:50Z">Offline Storage</delay><x xmlns="jabber:x:delay" stamp="20140919T10:41:50"></x></message>
答案 1 :(得分:2)
每当用户上线时,使用XMPPPresence发送用户在场。一旦ejabberd知道用户在场,它就会将所有离线消息发送回用户
答案 2 :(得分:0)
我也尝试从服务器获取所有消息,但无法获取。所以我使用XMPPMessageArchivingCoreDataStorage and XMPPMessageArchiving
首先声明变量
private var xmppMessageStorage: XMPPMessageArchivingCoreDataStorage?
private var stream = XMPPStream()
private var xmppMessageArchiving: XMPPMessageArchiving?
开始连接时
// for retrive old chat messages
xmppMessageStorage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
xmppMessageArchiving = XMPPMessageArchiving(messageArchivingStorage: xmppMessageStorage)
xmppMessageArchiving?.clientSideMessageArchivingOnly = true
xmppMessageArchiving?.activate(stream) //By this line all your messages are stored in CoreData
xmppMessageArchiving?.addDelegate(self, delegateQueue: DispatchQueue.main)
此方法用于在CoreData中获取特定用户的脱机消息
func getArchieveMessages(forUser user:String){
let fetchRequest = NSFetchRequest<NSFetchRequestResult>()
fetchRequest.predicate = NSPredicate(format: "bareJidStr = %@", user)
let context = xmppMessageStorage?.mainThreadManagedObjectContext
var messageEntity: NSEntityDescription? = nil
if let context = context {
messageEntity = NSEntityDescription.entity(forEntityName: "XMPPMessageArchiving_Message_CoreDataObject", in: context)
}
fetchRequest.entity = messageEntity
let sortDescriptor = NSSortDescriptor(key: "timestamp", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
var results: [Any]? = []
do {
results = try context?.fetch(fetchRequest)
self.modelController.delegate?.localHistoryMessages(results ?? [])
} catch let error {
debugPrint(error.localizedDescription)
}
debugPrint(results as Any)
}