Openfire+spark+smack Instant Messaging (issue one) offline messages are not available

Source: Internet
Author: User

The instant communication function has been developed, and now also wood has time to tidy up, and intends to take time after finishing the source and tutorial. Now to record the problems encountered, the first is to get offline messages.

Offlinemessagemanager is the official access to offline messages, but direct access, is not get any messages, you need to set up offline, and then get back online. The function code is as follows.

1. Offline

Initialize public xmppconnection init (loginconfig loginconfig) {connection.debug_enabled = false; Providermanager pm = providermanager.getinstance (); Configure (PM); connectionconfig = new Connectionconfiguration ( Loginconfig.getxmpphost (), Loginconfig.getxmppport (), Loginconfig.getxmppservicename ()); Connectionconfig.setsaslauthenticationenabled (false);//Do not use SASL authentication, set to Falseconnectionconfig.setsecuritymode ( ConnectionConfiguration.SecurityMode.enabled);//Allow automatic connection of connectionconfig.setreconnectionallowed (false); Connectionconfig.setsendpresence (false);//Set offline//After receiving a friend invitation manual said to be agreed, Accept_ All means roster.setdefaultsubscriptionmode automatically for friends without consent (Roster.SubscriptionMode.manual), connection = new Xmppconnection ( Connectionconfig); return connection;}

2. Get the message

Offlinemessagemanager Offlinemanager = new Offlinemessagemanager (connection); try {log.i ("Number of offline messages:", "" "+ Offlinemanager.getmessagecount ());iterator<org.jivesoftware.smack.packet.message> it = Offlinemanager.getmessages (); while (It.hasnext ()) {Org.jivesoftware.smack.packet.Message Message = It.next (); LOG.I ("Received offline message", "Received from" "+ message.getfrom () +" "Message:" + message.getbody ()); if (message! = NULL && m Essage.getbody ()! = null&&!message.getbody (). Equals ("null")) {immessage msg = new Immessage (); String time = (string) message.getproperty (immessage.key_time); Msg.settime (time = null?) Dateutil.getcurdatestr (): Time); Msg.setcontent (Message.getbody ()); if (Message.Type.error = = Message.gettype ()) { Msg.settype (immessage.error);} else {msg.settype (immessage.success);} String from = Message.getfrom (). Split ("/") [0];msg.setfromsubjid (from);//Generate notification Noticemanager Noticemanager = Noticemanager.getinstance (context); Notice Notice = new Notice () Notice.settitle ("new information");Tice.setnoticetype (notice.chat_msg); Notice.setcontent (Message.getbody ()); Notice.setfrom (from); notice.setStatus (Notice.unread); Notice.setnoticetime (time = null?) Dateutil.getcurdatestr (): time);//History Immessage Newmessage = new Immessage (); newmessage.setmsgtype (0); Newmessage.setfromsubjid (from); Newmessage.setcontent (Message.getbody ()); Newmessage.settime (time = null?) Dateutil.getcurdatestr (): Time); Messagemanager.getinstance (context). Saveimmessage (newmessage); Long Noticeid = Noticemanager.savenotice (notice); if (Noticeid! =-1) {Intent Intent = new Intent (constant.new_message_action); Intent.putextra (Immessage.immessage_key, msg); Intent.putextra ("Noticeid", Noticeid); Context.sendbroadcast (intent); Activitysupport.setnotitype (R.drawable.ic_ Launcher,context.getresources (). getString (R.string.new_message), Notice.getcontent (), Chatactivity.class, from);}} Offlinemanager.deletemessages ();//notifies the server to delete the offline message} catch (Exception e) {e.printstacktrace ();}


3, set up on-line:

Presence presence = new presence (Presence.Type.available);        Connection.sendpacket (presence);


Openfire+spark+smack Instant Messaging (issue one) offline messages are not available

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.