您的位置:首页 > 运维架构

openfire+spark+smack 即时通讯(问题篇一)离线消息获取不到

2014-11-05 11:11 357 查看
即时通信功能已经开发完毕,现在还木有时间整理,打算后期抽时间整理后发一下源码跟教程。现在记录一下遇到的问题,首先是获取离线消息。

OfflineMessageManager是官方获取离线消息的,但是直接获取,是获取不到任何消息的,需要先设置离线,然后获取后,再上线。功能代码如下。

1、离线

// 初始化
public XMPPConnection init(LoginConfig loginConfig) {
Connection.DEBUG_ENABLED = false;
ProviderManager pm = ProviderManager.getInstance();
configure(pm);

connectionConfig = new ConnectionConfiguration(
loginConfig.getXmppHost(), loginConfig.getXmppPort(),
loginConfig.getXmppServiceName());
connectionConfig.setSASLAuthenticationEnabled(false);// 不使用SASL验证,设置为false
connectionConfig
.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
// 允许自动连接
connectionConfig.setReconnectionAllowed(false);
connectionConfig.setSendPresence(false);// 设置离线
// 收到好友邀请后manual表示需要经过同意,accept_all表示不经同意自动为好友
Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.manual);
connection = new XMPPConnection(connectionConfig);
return connection;
}

2、获取消息
OfflineMessageManager offlineManager = new OfflineMessageManager(connection);
try {
Log.i("离线消息数量: ", "" + offlineManager.getMessageCount());
Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager.getMessages();

while (it.hasNext()) {
org.jivesoftware.smack.packet.Message message = it.next();
Log.i("收到离线消息", "Received from 【" + message.getFrom()
+ "】 message: " + message.getBody());
if (message != null && message.getBody() != null
&& !message.getBody().equals("null")) {
IMMessage msg = new IMMessage();
String time = (String) message
.getProperty(IMMessage.KEY_TIME);
msg.setTime(time == null ? DateUtil.getCurDateStr() : time);
msg.setContent(message.getBody());
if (Message.Type.error == message.getType()) {
msg.setType(IMMessage.ERROR);
} else {
msg.setType(IMMessage.SUCCESS);
}
String from = message.getFrom().split("/")[0];
msg.setFromSubJid(from);

// 生成通知
NoticeManager noticeManager = NoticeManager
.getInstance(context);
Notice notice = new Notice();
notice.setTitle("新信息");
notice.setNoticeType(Notice.CHAT_MSG);
notice.setContent(message.getBody());
notice.setFrom(from);
notice.setStatus(Notice.UNREAD);
notice.setNoticeTime(time == null ? DateUtil
.getCurDateStr() : time);

// 历史记录
IMMessage newMessage = new IMMessage();
newMessage.setMsgType(0);
newMessage.setFromSubJid(from);
newMessage.setContent(message.getBody());
newMessage.setTime(time == null ? DateUtil.getCurDateStr()
: time);
MessageManager.getInstance(context).saveIMMessage(
newMessage);

long noticeId = noticeManager.saveNotice(notice);
if (noticeId != -1) {
Intent intent = new Intent(Constant.NEW_MESSAGE_ACTION);
intent.putExtra(IMMessage.IMMESSAGE_KEY, msg);
intent.putExtra("noticeId", noticeId);
context.sendBroadcast(intent);
activitySupport.setNotiType(
R.drawable.ic_launcher,
context.getResources().getString(
R.string.new_message),
notice.getContent(), ChatActivity.class, from);
}
}
}

offlineManager.deleteMessages();// 通知服务器删除离线消息
} catch (Exception e) {
e.printStackTrace();
}

3、设置上线:

Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: