openfire+smack获取离线消息

ConnectionConfiguration connConfig = new ConnectionConfiguration("127.0.0.1");

//登录前先设置不发送在线状态,因offlineManager调用必须在用户状态为离线状态下获取,具体参照smack javadoc中的offlineManager类英文说明
connConfig.setSendPresence(false);
connection = new XMPPConnection(connConfig);
connection.connect();

OfflineMessageManager offlineManager = new OfflineMessageManager(sample.getConnection()); 
sample.login("test", "test");
//System.out.println(offlineManager.supportsFlexibleRetrieval()); //获取支持灵活的检索状态,正常应该是为true,个人理解为服务器的离线消息功能支持开关
System.out.println("离线消息数?: " + offlineManager.getMessageCount());
           
Iterator<Message> it = offlineManager.getMessages(); 
           
while (it.hasNext()) {
 Message message = it.next();
 System.out.println(message.getBody());
 }
   
offlineManager.deleteMessages();  //上报服务器已获取,需删除服务器备份,不然下次登录会重新获取

Presence presence = new Presence(Presence.Type.available);//此时再上报用户状态
sample.getConnection().sendPacket(presence);

posted @ 2013-04-26 14:27  wang_tale  阅读(1152)  评论(0编辑  收藏  举报