7

I have the following code which should listen for messages:

ChatManager chatmanager = ChatManager.getInstanceFor(xmppManager.getConnection());

chat = chatmanager.createChat(otherJabberId);

chat.addMessageListener(new ChatMessageListener() {
    @Override
    public void processMessage(Chat chat, Message message) {
        Log.e("message trigger", message.getBody());
    }
}

But my Log never triggers.

However, I have setDebuggerEnabled(true) in my code and the following shows up: 10-31 15:41:51.264 28889-28993/com.lfdversluis.buurapp D/SMACK: RECV (0): <message to="test@app.buur.nu/Smack" type="chat" id="53" from="testje@app.buur.nu/Gajim"><body>test</body><request xmlns="urn:xmpp:receipts"/><thread>277945c1-772a-4d4b-8e1a-274153cfb8a6</thread></message>

So the message is received. I have checked and the otherJabberId variable is correct. It is simply the listener not triggering. What's even more weird, sometimes it just works fine.

Another Issue I have is not being able to send messages.

Here, I have the chat setup as above and use the following code to send a message:

try {  
    chat.sendMessage(text.trim());  
    DataBaseManager db = new DataBaseManager(ChatActivity.this);  
    db.addMessageToDB(model);  

    addMessageToScreen(newMessage);  

} catch (SmackException.NotConnectedException ignored) {  
    XMPPManager manager = XMPPManager.getInstance();  
    manager.reconnect(); // Maybe we need to reconnect due to an interrupt and retry..  
    try {  
        chat.sendMessage(text.trim());  
        DataBaseManager db = new DataBaseManager(ChatActivity.this);  
        db.addMessageToDB(model);  
        addMessageToScreen(newMessage);  
    } catch (SmackException.NotConnectedException e) {  
        e.printStackTrace();  
        Toasteroid.show(ChatActivity.this, "Could not send message. Please try again.", Toasteroid.STYLES.ERROR);  
    }  
}  

And the toast with the "could not send message" pops-up every now and then. So apparently I am not connected and I cannot reconnect either?

So how can I make my connection more stable and make sure my messages get sent?

Gooey
  • 4,740
  • 10
  • 42
  • 76

2 Answers2

3

When preparing your connection (before you've called connect()) add a ChatManagerListener to the Connection. This ensures that chatCreated is called once for every new Chat. Within the chatCreated method add a ChatMessageListener to the Chat which is passed in; so that processMessage is called every time there's a message in that Chat.

ChatManager.getInstanceFor(mConnection).addChatListener(chatManagerListener);

ChatManagerListener chatManagerListener = new ChatManagerListener() {
    @Override
    public void chatCreated(Chat chat, boolean createdLocally) {
        chat.addMessageListener(
            new ChatMessageListener() {
                @Override
                public void processMessage(Chat chat, Message message) { }
            });
    }
});

As for your connection issues, you might want to add a org.jivesoftware.smack.ConnectionListener to your connection so you can make the failure a bit more transparent. You could also try to ensure auto-reconnect behaviour is enabled.

ReconnectionManager reconnectionManager = ReconnectionManager.getInstanceFor(mConnection);
if (!reconnectionManager.isAutomaticReconnectEnabled()) {
    reconnectionManager.enableAutomaticReconnection();        
}
fr1550n
  • 1,055
  • 12
  • 23
2

I think you need to update you code like this:-

chat.addMessageListener(new MessageListener() {
                 public void processMessage(Chat chat, Message message) {
                     System.out.println("Received message: "
                             + (message != null ? message.getBody() : "NULL"));
                 }
             });

Change ChatMessageListener() to MessageListener() , where MessageListener() is this -> org.jivesoftware.smack.MessageListener; I hope its work.

pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • Hmmm that could be an interesting change. It does work currently, but it is rather buggy. Maybe this will be better. I will try it out. – Gooey Nov 06 '15 at 14:02
  • I just tried and this cannot be done. ``addMessageListener()`` expects a ``ChatMessageListener``. – Gooey Nov 07 '15 at 12:36
  • This code is my Last chat app code which working for trigger chat message ! :) – pRaNaY Nov 10 '15 at 12:57
  • What instance is your chat object and which version of smack are you running? – Gooey Nov 10 '15 at 14:57
  • @Gooey i am facing same problem which u stated that addMessageListener() expects a ChatMessageListener. if you got solution then please let me know – ErShani May 12 '16 at 07:00