4

I am creating a chat application for Gtalk using the asmack api, and i want to get the messages when user goes offline, but gettting problems because of some problems in ServiceDiscoveryManager and its saying that feature not implemented(501) and tried to implements all the things which other users have same problem, but now i m getting this error. I am posting the code and logcat with this. any help appreciated.

 ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    host, Integer.parseInt(port), service);
 connConfig.setSASLAuthenticationEnabled(true);
 connConfig.setSendPresence(false);
 connection = new XMPPConnection(connConfig);
 connection.connect();
 connection.login(username, password);
 ServiceDiscoveryManager sdm= ServiceDiscoveryManager.getInstanceFor(connection);
 mOfflineMessageManager = new OfflineMessageManager(connection);
 offlinemsgs = mOfflineMessageManager.getMessageCount(); 

this is the code where I call for offline messages just after login, and below is the response in logcat error:

03-16 11:26:53.871: W/System.err(325): feature-not-implemented(501)
03-16 11:26:53.881: W/System.err(325):  at org.jivesoftware.smackx.OfflineMessageManager.getMessages(OfflineMessageManager.java:210)
03-16 11:26:53.881: W/System.err(325):  at com.apache.android.xmpp.MainScreen.getOfflinemessages(MainScreen.java:911)
03-16 11:26:53.881: W/System.err(325):  at com.apache.android.xmpp.MainScreen$LogIn.doInBackground(MainScreen.java:612)
03-16 11:26:53.881: W/System.err(325):  at com.apache.android.xmpp.MainScreen$LogIn.doInBackground(MainScreen.java:1)
03-16 11:26:53.881: W/System.err(325):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-16 11:26:53.881: W/System.err(325):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-16 11:26:53.881: W/System.err(325):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-16 11:26:53.881: W/System.err(325):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-16 11:26:53.881: W/System.err(325):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-16 11:26:53.891: W/System.err(325):  at java.lang.Thread.run(Thread.java:1096)

please help on this, trying to get the solution from many days but cant find any solution.

mayank_droid
  • 1,015
  • 10
  • 19

3 Answers3

8

try this :

 ConnectionConfiguration connConfig = new ConnectionConfiguration(
                    host, Integer.parseInt(port), service);
 connConfig.setSASLAuthenticationEnabled(true);
 connConfig.setSendPresence(false);
 connection = new XMPPConnection(connConfig);
 connection.connect();
 connection.login(username, password);
 ServiceDiscoveryManager sdm= ServiceDiscoveryManager.getInstanceFor(connection);

////////////////////////////

    OfflineMessageManager offlineManager = new OfflineMessageManager(  
                    Client.getConnection());  
            try {  
                Iterator<org.jivesoftware.smack.packet.Message> it = offlineManager  
                        .getMessages();  
                System.out.println(offlineManager.supportsFlexibleRetrieval());  
                System.out.println("Number of offline messages:: " + 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("receive offline messages, the Received from [" + message.getFrom()  
                                    + "] the 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);  
                    }  
                }  
                / / Deal with a collection of offline messages ...  
                Set<String> keys = offlineMsgs.keySet();  
                Iterator<String> offIt = keys.iterator();  
                while(offIt.hasNext())  
                {  
                    String key = offIt.next();  
                    ArrayList<Message> ms = offlineMsgs.get(key);  
                    TelFrame tel = new TelFrame(key);  
                    ChatFrameThread cft = new ChatFrameThread(key, null);  
                    cft.setTel(tel);  
                    cft.start();  
                    for (int i = 0; i < ms.size(); i++) {  
                        tel.messageReceiveHandler(ms.get(i));  
                    }  
                }  
                offlineManager.deleteMessages();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  

See this Like : http://community.igniterealtime.org

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • as there is no any other answer and your answer seem to quite almost right, I am just getting problem in google services but not from asmack api. I should appreciate your answer. – mayank_droid Mar 27 '12 at 10:41
  • @imran khan what is the best Api for xmpp client have to support both facebook and gtalk chat's. if i use asmack-issue5 facing problem for gtalk and smack facing problem for gtalk (getting buddy list).. – RajaReddy PolamReddy Jul 27 '12 at 10:42
  • Thanks but why do we need to send the presence as false. that means i am in offline(my available status in the server is offline). but we need to get the offline messages when i am in online only right? that means i have to get the offline messages in next log in session. for this purpose what should i do?? @pρяσѕρєя K – Roster Aug 16 '13 at 12:56
  • @prospec can you tell me about Client.getconnection?? new OfflineMessageManager( Client.getConnection()); Client.getConnection() cannot found Client? – CoronaPintu May 18 '15 at 13:07
  • This works fine...my issue was with setting connConfig.setSendPresence(true); – mut tony Jul 02 '20 at 18:45
0

Why u not just use PacketListener i'm using it and getting offline messages .here's my code i'm saving also messages into Sqlite db to get user messages after awhile when he left this chat and chat with another friend and then go back.

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            Main.conn.addPacketListener(new PacketListener() {

                public void processPacket(Packet p) {

                    message = (Message) p;// this was the problem

                    if (message.getBody() != null) {
                        messages.add(message.getBody());
                        Log.i("XMPPClient",
                                "Reciveing text [" + message.getBody() + "] ");

                        time.add(TimeDate());

                        // Add the incoming message to the list view
                        mHandler.post(new Runnable() {
                            public void run() {
                                // / saving convertsations into SQLite db
                                try {

                                    save.open();
                                    save.InsertIntoDatabase(jid, messages, time);
                                    messagesDB = save.GetAllValues(Table_Name,
                                            new String[] { "messages" },
                                            friendJid);
                                    timeDB = save.GetAllValues(Table_Name,
                                            new String[] { "time" }, friendJid);
                                    adapter = new ConversationListAdapter(
                                            getApplicationContext(),
                                            messagesDB, timeDB);
                                    list.setAdapter(adapter);
                                    adapter.notifyDataSetChanged();
                                    save.close();

                                } catch (SQLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }


                            }
                        });
                    }
                }

            }, filter);
        }
Weloo
  • 625
  • 4
  • 16
  • thanks for this:). But i am looking for offline messages which i am not getting when i start the app, there should be something from which i could retrieve offline messages, i have already tried to get them from this method, but nothing working. If u know about how to get offline messages then please guide me. – mayank_droid May 03 '12 at 05:24
  • @mak_just4anything Hi have you find the soln :( I am still searching :( – Gaurav Arora Nov 04 '12 at 08:45
0

I was facing the same problem developing an Android client with aSmack connected to an Openfire server: I simply can't retrive offline messages. So reading the [XEP-0013]: http://www.xmpp.org/extensions/xep-0013.html I figured out why:

"Upon receiving a service discovery request addressed to a node of "http://jabber.org/protocol/offline" (either a disco#info request as in this use case or a disco#items request as in the next use case), the server MUST NOT send a flood of offline messages if the user subsequently sends initial presence to the server during this session. Thus the user is now free to send initial presence (if desired) and to engage in normal IM activities while continuing to read through offline messages."

So, what I learn:

  1. Check if your server support Flexible Offline Message Retrieval XEP-0013;
  2. You must initialize SmackAndroid.init(Context) - See aSmack README;
  3. You must use AndroidConnectionConfiguration instead of ConnectionConfiguration;
  4. You can set your configuration to setSendPresence(true) if you need.

In order to get the things working you must connect, send initial presence as "unavalilable" in order to retrieve your messages, then after that send presence as "available".

Then your code should looks like this (try/catch blocks ommited):

SmackAndroid.init(context);
AndroidConnectionConfiguration connConfig = null;
connConfig = new AndroidConnectionConfiguration(domain, PORT);
connConfig.setSendPresence(true);
connConfig.setReconnectionAllowed(true);
connection = new XMPPConnection(connConfig);
connection.connect();
connection.login("username", "password");

PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
//RECEIVER
connection.addPacketListener(getPacketListener(), filter);
//in order to retrieve offline messages: XEP-0013
connection.sendPacket(new Presence(Presence.Type.unavailable));
connection.sendPacket(new Presence(Presence.Type.available));

Then after send Presence.Type.unavailable packet, all offline messages were sent automaticaly!