Receive offline messages in an Android chat application using the asmack library

I am using asmack library for my chat application. I am using the code below to receive a messagecount:

ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(connection); DiscoverInfo info = manager.discoverInfo(null,"http://jabber.org/protocol/offline"); Form extendedInfo = Form.getFormFrom(info); if (extendedInfo != null) { String value = extendedInfo.getField("number_of_messages").getValues().next(); Log.e(TAG, "offline message"+value); } 

But I got an exception as described below:

 09-14 16:11:32.892: ERROR/AndroidRuntime(498): at com.datingapps.breaktheice.SigninScreen.coonectToChat(SigninScreen.java:393) 09-14 16:35:20.611: WARN/System.err(538): java.lang.ClassCastException: org.jivesoftware.smack.util.PacketParserUtils$2 09-14 16:35:20.811: WARN/System.err(538): at org.jivesoftware.smackx.ServiceDiscoveryManager.discoverInfo(ServiceDiscoveryManager.java:608) 09-14 16:35:20.811: WARN/System.err(538): at com.datingapps.breaktheice.Chat$connectionTask.doInBackground(Chat.java:286) 09-14 16:35:20.811: WARN/System.err(538): at com.datingapps.breaktheice.Chat$connectionTask.doInBackground(Chat.java:1) 09-14 16:35:20.811: WARN/System.err(538): at android.os.AsyncTask$2.call(AsyncTask.java:185) 09-14 16:35:20.811: WARN/System.err(538): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 09-14 16:35:20.821: WARN/System.err(538): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 09-14 16:35:20.821: WARN/System.err(538): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 09-14 16:35:20.821: WARN/System.err(538): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 09-14 16:35:20.821: WARN/System.err(538): at java.lang.Thread.run(Thread.java:1096) 

Anyone who has implemented it using asmack or has an alternative solution with smack lib, Please help me solve this problem.

+2
source share
6 answers

After googling and searching the documentation, all I got was offlinemanager to receive messages offline.

But it does not work in asmack or maybe in smack.It always returns 0 message.

Finally, when I saw the logs, I found that every tie, when I logged in, received a lot of responses on the chat server, which also contains an offline message, but with a message tag, not an offline message tag. So, I finally found

You can receive offline messages from it by directly installing the packet listener after logging in. As below, you must implement the packet listener after the login method.

  PacketFilter filter = new MessageTypeFilter(Message.Type.chat); this.connection.addPacketListener(new PacketListener() { public void processPacket(Packet packet) { Message message = (Message) packet; if (message.getBody() != null) { String fromName = StringUtils.parseBareAddress(message .getFrom()); Log.i("XMPPClient", "Got text [" + message.getBody() + "] from [" + fromName + "]"); if (fromName.equalsIgnoreCase(matchUserJabberId + "server name")) { // } } } } }, filter); 

Hope this helps a lot of people find work for offline messaging sooner since I spent more time getting it.

+7
source

Replace your code with this. I used this code to work it.

 public int offlinemessagecount(){ try { ServiceDiscoveryManager manager = ServiceDiscoveryManager .getInstanceFor(connection); DiscoverInfo info = manager.discoverInfo(null, "http://jabber.org/protocol/offline"); Form extendedInfo = Form.getFormFrom(info); if (extendedInfo != null) { String value = extendedInfo.getField("number_of_messages") .getValues().next(); return Integer.parseInt(value); } } catch (Exception e) { e.printStackTrace(); } return 0; } 
+4
source

GTalkSMS is a good example for every basic operation, using Asmack lib the following is a small example of the code that I took in the form of GTalkSMS to receive messages offline.

 public static void handleOfflineMessages(XMPPConnection connection, Context ctx)throws Exception { OfflineMessageManager offlineMessageManager = new OfflineMessageManager(connection); if (!offlineMessageManager.supportsFlexibleRetrieval()) { Log.d("Offline messages not supported"); return; } if (offlineMessageManager.getMessageCount() == 0) { Log.d("No offline messages found on server"); } else { List<Message> msgs = offlineMessageManager.getMessages(); for (Message msg : msgs) { String fullJid = msg.getFrom(); String bareJid = StringUtils.parseBareAddress(fullJid); String messageBody = msg.getBody(); if (messageBody != null) { Log.d("Retrieved offline message from " +messageBody); } } offlineMessageManager.deleteMessages(); } } 
+1
source

Try replacing your code with this.

 ProviderManager mgr = ProviderManager.getInstance(); mgr.addExtensionProvider(offline, http://jabber.org/protocol/offline, org.jivesoftware.smackx.packet.OfflineMessageInfo$Provider); mgr.addIQProvider(offline, http://jabber.org/protocol/offline, org.jivesoftware.smackx.packet.OfflineMessageRequest$Provider); OfflineMessageManager offMgr = new OfflineMessageManager(connection); int numOffline = offMgr.getMessageCount(); 
0
source

Just check if you configured your smack providers?

Before you establish a connection, you should call it

 SmackAndroid.init(mContext); // this loads extension providers into aSmack try { conn.connect(); } catch (XMPPException e) { e.printStackTrace(); // do something } 

Without causing this, you will definitely get

0
source

Use the OfflineMessageManager offline mode if you do not want to receive offline messages automatically (XEP-0013).

Otherwise, just add your StanzaListener to the connection and log in.

  XMPPTCPConnectionConfiguration userBasicConfiguration = XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() .setServiceName(SERVICE_NAME) .setHost(HOST_NAME) .setDebuggerEnabled(false) .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) .setPort(PORT) .setUsernameAndPassword(userName, password) .build(); AbstractXMPPConnection firstUserConnection = new XMPPTCPConnection(userBasicConfiguration); firstUserConnection.connect(); StanzaFilter filter = new StanzaTypeFilter(Message.class); StanzaListener listener = stanza -> { System.out.println(stanza.toString()); }; firstUserConnection.addSyncStanzaListener(listener,filter); firstUserConnection.login(); while (!Thread.currentThread().isInterrupted()){ } firstUserConnection.disconnect(); 
0
source

Source: https://habr.com/ru/post/1401745/


All Articles