private void processOfflineMessages() { Log.i(LOG_TAG, "Begin retrieval of offline messages from server"); OfflineMessageManager offlineMessageManager = new OfflineMessageManager(con); try { if (!offlineMessageManager.supportsFlexibleRetrieval()) { Log.d(LOG_TAG, "Offline messages not supported"); return; } List<Message> msgs = offlineMessageManager.getMessages(); for (Message msg : msgs) { Intent intent = new Intent(MessageService.ACTION_MESSAGE_RECEIVED, null, context, MessageService.class); intent.putExtra( MessageService.EXTRA_DATA_NAME_FROM, StringUtils.parseBareAddress(msg.getFrom())); intent.putExtra(MessageService.EXTRA_DATA_NAME_MESSAGE_BODY, msg.getBody()); context.startService(intent); } offlineMessageManager.deleteMessages(); } catch (Exception e) { Log.e(LOG_TAG, "handle offline messages error ", e); } Log.i(LOG_TAG, "End of retrieval of offline messages from server"); }
public static void main(String[] args) { SmackConfiguration.DEBUG = true; SmackConfiguration.setDefaultPacketReplyTimeout(10 * 1000); XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder(); configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); configBuilder.setUsernameAndPassword("registeruser2", "123456"); configBuilder.setServiceName("ejabberddemo.com"); configBuilder.setDebuggerEnabled(true).build(); // 必须在用户状态为离线状态 configBuilder.setSendPresence(false); AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build()); try { connection.connect(); } catch (SmackException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XMPPException e) { e.printStackTrace(); } // FIXME OfflineMessageManager offlineManager = new OfflineMessageManager(connection); try { connection.login(); } catch (XMPPException e) { e.printStackTrace(); } catch (SmackException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { System.out.println(offlineManager.supportsFlexibleRetrieval()); System.out.println(offlineManager.getMessageCount()); } catch (SmackException.NoResponseException e) { e.printStackTrace(); } catch (XMPPException.XMPPErrorException e) { e.printStackTrace(); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } // 上线 Presence presence = new Presence(Presence.Type.available); try { connection.sendPacket(presence); } catch (SmackException.NotConnectedException e) { e.printStackTrace(); } }