本文主要是介绍#xmpp笔记# Android获取openfire离线消息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android实现openfire获取离线消息的基本思路:
在用户连接登录openfire之前,先连接一次openfire,并且要配置ConnectionConfiguration,一定要设置SendPresence为false,即将在线状态设置为离线,然后才能接收到离线消息,处理完离线消息之后,记得要通知openfire服务器端,删除接收到的离线消息,并且设置再将状态设置为在线
private void getOffline(){ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT);// 允许自动连接connConfig.setReconnectionAllowed(true);connConfig.setSendPresence(false);//不要告诉服务器自己的状态Connection connection = new XMPPConnection(connConfig);try {connection.connect();// 开启连接connection.login(username,password,XMPP_RESOURCE_NAME);} catch (XMPPException e) {throw new IllegalStateException(e);}OfflineMessageManager offlineManager = new OfflineMessageManager(connection);try {Iterator<Message> it = offlineManager.getMessages();Map<String, ArrayList<Message>> offlineMsgs = new HashMap<String, ArrayList<Message>>();while (it.hasNext()) {Message message = it.next();//对离线消息进行的处理NotifyChatMessage chatMessage = new NotifyChatMessage(XmppManager.this);chatMessage.notifyMessage(message.getBody());}offlineManager.deleteMessages();//删除所有离线消息} catch (Exception e) {e.printStackTrace();}Presence presence = new Presence(Presence.Type.available);connection.sendPacket(presence);//上线了connection.disconnect();//关闭连接}
这篇关于#xmpp笔记# Android获取openfire离线消息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!