文章只是选取了其中一段,无XMPP基础的人可能看起来有点复杂;

假设我们注册了一个用户,用户名叫shimiso,那么我们如何让shimiso这个用户一登陆就取到离线消息呢?

PPConnection.DEBUG_ENABLED = false;
AccountManager accountManager;
final ConnectionConfiguration connectionConfig = new ConnectionConfiguration(
"192.168.1.78", Integer.parseInt("5222"), "csdn.shimiso.com"); // 允许自动连接
connectionConfig.setReconnectionAllowed(true);
connectionConfig.setSendPresence(false);//不要告诉服务器自己的状态
Connection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();// 开启连接
accountManager = connection.getAccountManager();// 获取账户管理类
} catch (XMPPException e) {
throw new IllegalStateException(e);
}
connection.login("shimiso", "123","SmackTest");
OfflineMessageManager offlineManager = new OfflineMessageManager(
connection);
try {
Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager
.getMessages(); System.out.println(offlineManager.supportsFlexibleRetrieval());
System.out.println("离线消息数量: " + offlineManager.getMessageCount()); Map<String, ArrayList<Message>> offlineMsgs = new HashMap<String, ArrayList<Message>>(); while (it.hasNext()) {
org.jivesoftware.smack.packet.Message message = it.next();
System.out
.println("收到离线消息, Received from 【" + message.getFrom()
+ "】 message: " + message.getBody());
String fromUser = message.getFrom().split("/")[0]; if (offlineMsgs.containsKey(fromUser)) {
offlineMsgs.get(fromUser).add(message);
} else {
ArrayList<Message> temp = new ArrayList<Message>();
temp.add(message);
offlineMsgs.put(fromUser, temp);
}
} // 在这里进行处理离线消息集合......
Set<String> keys = offlineMsgs.keySet();
Iterator<String> offIt = keys.iterator();
while (offIt.hasNext()) {
String key = offIt.next();
ArrayList<Message> ms = offlineMsgs.get(key); for (int i = 0; i < ms.size(); i++) {
System.out.println("-->" + ms.get(i));
}
} offlineManager.deleteMessages();
} catch (Exception e) {
e.printStackTrace();
}
offlineManager.deleteMessages();//删除所有离线消息
Presence presence = new Presence(Presence.Type.available);
nnection.sendPacket(presence);//上线了
nnection.disconnect();//关闭连接

运行结果:

这里我们需要特别当心的是先不要告诉openfire服务器你上线了,否则永远也拿不到离线消息,用下面老外的话将就是在你上线之前去获取离线消息,这么设计是很有道理的。

The
OfflineMessageManager helps manage offline messages even before the user
has sent an available presence. When a user asks for his offline
messages before sending an available presence then the server will not
send a flood with all the offline messages when the user becomes
online. The server will not send a flood with all the offline messages
to the session that made the offline messages request or to any other
session used by the user that becomes online.

同时拿到离线消息以后删除离线消息offlineManager.deleteMessages();,同是通知服务器自己上线了。

https://blog.csdn.net/kazeik/article/details/44344609

Xmpp获取离线消息的更多相关文章

  1. asmack xmpp 获取离线消息

    原文:http://plplum.blog.163.com/blog/static/31032400201503015345948/ 注意事项: 1.登录前要将状态设置为离线: ConnectionC ...

  2. XMPP——Smack[6]离线消息和离线文件的实现

    终篇,三天所学所用,也就这些,如果需要大家要自己去查资料研究研究,功能其实可以很强大的 可惜界面做得不好,一大短处,从大一迄今没整好,主要是个人审美不行,哎 毕业季呀毕业季,明天摆摊卖书,再半月就可能 ...

  3. openfire接收离线消息

    先接收离线消息后再通知openfire上线 //获取离线消息 OfflineMessageManager offlineMessageManager=new OfflineMessageManager ...

  4. XMPP——Smack[5]文件传输及离线消息的获取

    三天时间,赶在最后一下午实现了文件的传输,本来需要实现离线文件的发送的,一直没想好怎么弄,找openfire的离线文件插件没找到,后来想出一种方法,起服务器时起了一个系统用户,一直在线,当用户发送离线 ...

  5. XMPP系列(四)---发送和接收文字消息,获取历史消息功能

    今天开始做到最主要的功能发送和接收消息.获取本地历史数据. 先上到目前为止的效果图:              首先是要在XMPPFramework.h中引入数据存储模块: //聊天记录模块的导入 # ...

  6. 基于ejabberd简单实现xmpp群聊离线消息

    首先,xmpp服务器是基于ejabberd.离线消息模块是mod_interact,原地址地址:https://github.com/adamvduke/mod_interact: 修改后实现群聊离线 ...

  7. xmpp和OpenFire示例,即时聊天室,支持离线消息

    让我说说为什么写这个博客,这是因为我在上周末的研究XMPP和OpenFire,从互联网上下载Demo,但跑不起来.它花了很长的时间.它被改造.抬高.篇博文也是希望后边学习XMPP和OpenFire的同 ...

  8. Asmack离线消息时间获取

    DelayInformation info = (DelayInformation)message.getExtension("x","jabber:x:delay&qu ...

  9. IM消息送达保证机制实现(二):保证离线消息的可靠投递

    1.前言 本文的上篇<IM消息送达保证机制实现(一):保证在线实时消息的可靠投递>中,我们讨论了在线实时消息的投递可以通过应用层的确认.发送方的超时重传.接收方的去重等手段来保证业务层面消 ...

随机推荐

  1. 在python3下使用requests,xpath,urllib爬取不得姐网站相关视频爬虫源代码

    #coding=utf-8 from lxml import etreeimport requestsimport urllibimport os # 获取url的html等内容def getHtml ...

  2. HDU 5391 Zball in Tina Town【威尔逊定理】

    <题目链接> Zball in Tina Town Problem Description Tina Town is a friendly place. People there care ...

  3. hdu-1754 I Hate It【线段树】(求区间最大值)

    <题目链接> I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. 洛谷 P1474 货币系统 Money Systems(经典)【完全背包】+【恰好装满的最大方案数量】

    题目链接:https://www.luogu.org/problemnew/show/P1474 题目描述 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对 ...

  5. 【Android】窗口机制分析与UI管理系统

    类图关系 在看Android的窗口机制之前,先看看其主要的类图关系以及层级之间的依赖与调用关系 1.window在当前的android系统的中的呈现形式是PhoneWindow (frameworks ...

  6. Python3练习题系列(09)——物以类聚,人以群分

    目标: 用类管理同类事物 解析: 用到“class”的编程语言被称作“Object Oriented Programming(面向对象编程)”语言.首先你需要做出“东西”来,然后你“告诉”这些东西去完 ...

  7. [HDU1598]find the most comfortable road

    思路: 考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径.相当于判$S$和$T$是否连通,用并查集连一下即可.时 ...

  8. python-memcached包使用方法

    本文转载自:http://my.oschina.net/flynewton/blog/10660 将memcached.pyc拷贝到工作目录 #!/usr/bin/env python import ...

  9. 95. 不同的二叉搜索树 II

    95. 不同的二叉搜索树 II 题意 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 解题思路 这道题目是基于不同的二叉搜索树进行改进的: 对于连续整数序列[left, ri ...

  10. AJAX传输——以XML文件传输为例

    此文档解决以下问题: 一.responseText获取数据 1.AJAX异步传输,get请求方式/post请求方式,输出全部xml数据 二.responseXML获取数据 2.AJAX异步传输,get ...