Smack message listener not called and connection unstable
Asked Answered
G

2

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="[email protected]/Smack" type="chat" id="53" from="[email protected]/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?

Grewitz answered 31/10, 2015 at 14:50 Comment(0)
S
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();        
}
Saucepan answered 9/4, 2016 at 1:43 Comment(0)
A
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.

Abatis answered 4/11, 2015 at 6:25 Comment(5)
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.Grewitz
I just tried and this cannot be done. addMessageListener() expects a ChatMessageListener.Grewitz
This code is my Last chat app code which working for trigger chat message ! :)Abatis
What instance is your chat object and which version of smack are you running?Grewitz
@Grewitz i am facing same problem which u stated that addMessageListener() expects a ChatMessageListener. if you got solution then please let me knowShirleeshirleen

© 2022 - 2024 — McMap. All rights reserved.