How to receive Incoming XMPP Messages using Smack?

Winter picture Winter · Feb 14, 2011 · Viewed 29.1k times · Source

I read some examples and tested them but all of them need to start a chat with someone first to receive Incoming Messages... I want to retrieve this Incoming Messages without need to talk first to the jid anyone can give an example ?

Answer

Chris picture Chris · Feb 14, 2011

You need to register a ChatListener to be notified of new chats, then you can add a message listener to them like normal:

connection.getChatManager().addChatListener(new ChatManagerListenerImpl());

....

private class ChatManagerListenerImpl implements ChatManagerListener {

    /** {@inheritDoc} */
    @Override
    public void chatCreated(final Chat chat, final boolean createdLocally) {
        chat.addMessageListener(...);
    }

}