#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();//关闭连接
}
在用户连接登录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();//关闭连接
}
相关文章推荐
- openfire,XMPP,smack,android下离线消息的接收
- Android xmpp开发 asmack获取离线在线添加好友消息 及 好友上线下线通知
- Android xmpp开发 asmack获取离线在线添加好友消息 及 好友上线下线通知
- Android xmpp开发 asmack获取离线在线添加好友消息 及 好友上线下线通知
- XMPP——Smack[5]文件传输及离线消息的获取
- PHP 获取MSN好友列表 组信息 发送离线消息 (二)
- XMPP——Smack[5]文件传输及离线消息的获取
- XMPP——Smack[5]文件传输及离线消息的获取 (转)
- XMPP——Smack[5]文件传输及离线消息的获取(转)
- openfire插件最佳实践(四)离线消息推送ios服务器
- Openfire 离线消息的处理机制
- PHP 获取MSN好友列表 组信息 发送离线消息 (一)
- XMPP——Smack[5]文件传输及离线消息的获取
- XMPP——Smack[5]文件传输及离线消息的获取
- Android通讯录管理(获取联系人、通话记录、短信消息)(二)
- Android通讯录管理(获取联系人、通话记录、短信消息)
- PHP 获取MSN好友列表 组信息 发送离线消息 (三)
- android 短信管理(获取接受到短信的消息)
- Openfire 离线消息的处理机制
- openfire离线消息乱码(XMPP)