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