您的位置:首页 > 移动开发 > Android开发

#xmpp笔记# Android获取openfire离线消息

2017-04-06 15:05 351 查看
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();//关闭连接

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息