使用asmack检索脱机消息 [英] Retrieving offline messages using asmack

查看:98
本文介绍了使用asmack检索脱机消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asmack和openfire的新手,为这个问题找到了一个可行的答案,但找不到任何地方.如何通过asmack登录到我的帐户中检索离线消息?

I'm new to asmack and openfire, looked a lot for a working answer to this but couldn't find it anywhere. How do I retrieve offline messages on Logging into my account on asmack?

我使用了以下代码:

      configure(ProviderManager.getInstance()); //configuring providers before creating a connection

            ConnectionConfiguration connConfig =  new ConnectionConfiguration(HOST, PORT);

            connConfig.setSendPresence(false);

      connection =  new  XMPPConnection (connConfig); 


     try {
       connection.connect();


     } catch (XMPPException ex) {

         setConnection(null);
     }
      try {


        connection.login(username, password);


        try { 
                  OfflineMessageManager offlineManager = new OfflineMessageManager( 
                    connection);
            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 ... 

            offlineManager.deleteMessages(); 
        } catch (Exception e) { 
                  Log.e("CATCH","OFFLINE");
            e.printStackTrace(); 
        }

        Presence presence = new Presence(Presence.Type.available);

        connection.sendPacket(presence);
        setConnection(connection);//Packet Listener






        // Set the status to available



          } catch (XMPPException ex) {

            setConnection(null);
          }

据我所读,建立连接后,Openfire会自动将脱机消息发送给用户. (如果有的话)这意味着通过仅在登录后设置数据包侦听器,我应该能够检索到消息.但是,这对我不起作用.这就是为什么我尝试使用OfflineMessageManager的原因.但是,它始终显示0条消息作为消息计数.我什至登录到服务器正在使用的mysql db并检查了离线消息文件夹.消息一直存在,直到用户登录为止,这意味着正在发送消息,但应用程序未检索到消息.我似乎不知道如何实现这一目标.如果有人有可行的解决方案,将不胜感激.

From what I've read, once a connection is established, Openfire automatically sends offline messages to the user. (if any) Which means that by just setting packet listeners after logging in, I should be able to retrieve the messages. However, this didn't work for me. Which's why I tried using OfflineMessageManager. It always shows 0 messages as the message count though. I even logged in to the mysql db which the server's using and checked the offline messages folder. The messages exist till the user logs in, which means that the messages are being sent but it isn't being retrieved by the app. I can't seem to find out how implement this. If anyone has a working solution, It'd be greatly appreciated.

推荐答案

发送您的状态后登录到XMPP服务器.您已经忘记添加packetListener来侦听即将到来的脱机消息. 希望这行得通.

Send your presence afetr logging in to the XMPP server. You have forgot to add packetListener which listens for the upcoming offline messages. Hope this works.

这篇关于使用asmack检索脱机消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆