XMPP series (iv)---Send and receive text messages for historical message functionality

Source: Internet
Author: User

Start today to do the most important functions to send and receive messages, to get local historical data.

First up to date:


The first is to introduce a data storage module into the XMPPFramework.h:

Import of the chat log module #import "XMPPMessageArchiving.h" #import "XMPPMessageArchivingCoreDataStorage.h" #import " Xmppmessagearchiving_contact_coredataobject.h "//Closest contact #import" Xmppmessagearchiving_message_coredataobject.h "

The data storage module is then added to the Xmppstream because the XMPP framework defaults to storing the data with CoreData, and the data is stored, so we just need to send the notification processing information in the right place.

4. Message module, here with a single case, can not switch account login, otherwise there will be data problems.        _xmppmessagearchivingcoredatastorage = [Xmppmessagearchivingcoredatastorage sharedinstance];        _xmppmessagearchiving = [[Xmppmessagearchiving alloc] Initwithmessagearchivingstorage:_ Xmppmessagearchivingcoredatastorage Dispatchqueue:dispatch_get_global_queue (Dispatch_queue_priority_background, 9)];        [_xmppmessagearchiving Activate:self.xmppStream];

After the data store is complete, send a notification:

in the Xmppmessagearchivingcoredatastorage's

-(void) archivemessage: ( Xmppmessage *) Message outgoing: (BOOL) isoutgoing xmppstream: (xmppstream *) Add the following notification at the end of the Xmppstream:

    Dispatch_async (Dispatch_get_main_queue (), ^{        [[Nsnotificationcenter Defaultcenter] Postnotificationname: Kxmpp_message_change Object:nil];    });
Now go to the chat interface to get historical news:

/** Query chat record */-(void) getchathistory{xmppmessagearchivingcoredatastorage *storage = [Jkxmpptool SHAREDINSTANCE].XMPPME    Ssagearchivingcoredatastorage;    Query the time to give context nsfetchrequest *fetchrequest = [[Nsfetchrequest alloc] init]; Nsentitydescription *entity = [nsentitydescription entityForName:storage.messageEntityName inmanagedobjectcontext:    Storage.mainthreadmanagedobjectcontext];    [Fetchrequest setentity:entity]; Specify criteria for filtering which objects to fetch nspredicate *predicate = [nspredicate predicatewithformat:@ "ba    Rejidstr =%@ ", Self.chatJID.bare];    [Fetchrequest Setpredicate:predicate]; Specify how the fetched objects should be sorted nssortdescriptor *sortdescriptor = [[Nssortdescriptor alloc] Initwi    thkey:@ "timestamp" ascending:yes];        [Fetchrequest Setsortdescriptors:[nsarray Arraywithobjects:sortdescriptor, Nil]];    Nserror *error = nil; Nsarray *fetchedobjects = [Storage.mainthreadmanagedobjectcontext executefetchrequest:fetchrequest error:&error];        if (fetchedobjects! = nil) {self.chathistory = [[Nsmutablearray alloc] initwitharray:fetchedobjects];    [Nsmutablearray arraywitharray:fetchedobjects];        } [Self.messagetableview Reloaddata]; [Self tableviewscrolltobottom];}

message ingestion is also called Getchathistory method can be


And the message is sent:

/** sent event */-(void) sendmessage{    if (_chattextfield.text.length < 1) {        return;    }    Xmppmessage *message = [xmppmessage messagewithtype:@ "chat" To:self.chatJID];    [Message Addbody:_chattextfield.text];    [[Jkxmpptool Sharedinstance].xmppstream sendelement:message];        _chattextfield.text = @ "";        [Self tableviewscrolltobottom];}

Demo Address: Https://github.com/Joker-King/ChatDemo

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

XMPP series (iv)---Send and receive text messages for historical message functionality

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.