XMPPFramework - Retrieving Openfire Message Archives

后端 未结 4 698
醉梦人生
醉梦人生 2020-12-30 17:40

Spent hours trying to solve this problem and I\'m stumped!

Trying to grab the Chat History between 2 users on my OpenFire server and I read that I plugin was needed

4条回答
  •  没有蜡笔的小新
    2020-12-30 18:18

    If you are looking for a chat history, I think you have to save the messages to core data and retrieve them from there. For saving data using the XMPPFramework inbuilt functionality, you have to use this code:

    XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage   sharedInstance];
    NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
    
    xmppMessageArchivingStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
    xmppMessageArchivingModule = [[XMPPMessageArchiving alloc] initWithMessageArchivingStorage:xmppMessageArchivingStorage];
    [xmppMessageArchivingModule activate:xmppStream];
    [xmppMessageArchivingModule  addDelegate:self delegateQueue:dispatch_get_main_queue()];
    

    Now you have to retrieve that messages from core data by this:

    -(void)loadarchivemsg
    {  
        XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
        NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
                                                          inManagedObjectContext:moc];
        NSFetchRequest *request = [[NSFetchRequest alloc]init];
    
        NSString *predicateFrmt = @"bareJidStr like %@ ";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFrmt, chatWithUser];
        request.predicate = predicate;
        NSLog(@"%@",[[NSUserDefaults standardUserDefaults] stringForKey:@"kXMPPmyJID"]);
        [request setEntity:entityDescription];
        NSError *error;
        NSArray *messages_arc = [moc executeFetchRequest:request error:&error];
    
        [self print:[[NSMutableArray alloc]initWithArray:messages_arc]];
    }
    
    -(void)print:(NSMutableArray*)messages_arc{
        @autoreleasepool {
            for (XMPPMessageArchiving_Message_CoreDataObject *message in messages_arc) {
                NSXMLElement *element = [[NSXMLElement alloc] initWithXMLString:message.messageStr error:nil];
                NSLog(@"to param is %@",[element attributeStringValueForName:@"to"]);
    
                NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
                [m setObject:message.body forKey:@"msg"];
    
                if ([[element attributeStringValueForName:@"to"] isEqualToString:chatWithUser]) {               
                    [m setObject:@"you" forKey:@"sender"];
                }
                else {
                    [m setObject:chatWithUser forKey:@"sender"];
                }
    
                [messages addObject:m];
    
                NSLog(@"bareJid param is %@",message.bareJid);
                NSLog(@"bareJidStr param is %@",message.bareJidStr);
                NSLog(@"body param is %@",message.body);
                NSLog(@"timestamp param is %@",message.timestamp);
                NSLog(@"outgoing param is %d",[message.outgoing intValue]);
                NSLog(@"***************************************************");
            }
        }
    }
    

提交回复
热议问题