Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smack message listener not called and connection unstable

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?

like image 289
Gooey Avatar asked Oct 31 '15 14:10

Gooey


2 Answers

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();        
}
like image 181
fr1550n Avatar answered Nov 19 '22 20:11

fr1550n


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.

like image 2
pRaNaY Avatar answered Nov 19 '22 19:11

pRaNaY