I'm writing chat application using XMPP and Smack Android library. I'm sending messages using code below and everything is working fine.
final ChatManager chatManager = ChatManager.getInstanceFor(connection);
chatManager.addChatListener(this);
....
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
chat.addMessageListener(this);
}
@Override
public void processMessage(Chat chat, Message message) {
// Do something here.
}
Chat chat = ChatManager.getInstanceFor(connection).createChat(jid);
chat.sendMessage("message");
Unfortunately the API above is deprecated org.jivesoftware.smack.chat.Chat
and instead I should use org.jivesoftware.smack.chat2.Chat
, so I am changing implementation as follows
final ChatManager chatManager = ChatManager.getInstanceFor(connection);
chatManager.addOutgoingListener(this);
chatManager.addIncomingListener(this);
....
Chat chat = ChatManager.getInstanceFor(connection).chatWith(jid);
chat.send("message");
In this case I can still get Incoming messages, but when I am trying to send message with chat.send("message");
server does not get anything and addOutgoingListener
callback is not called.
Any ideas why?
Digging a bit deeper I found the answer, the code below will help to send a message
final Chat chat = ChatManager.getInstanceFor(connection).chatWith(jid);
Message newMessage = new Message(jid, Message.Type.chat);
newMessage.setBody(message);
chat.send(newMessage);
So instead of sending a string message, you need to create a Message
object and I think what is more important is to specify Message.Type.chat
in the constructor and also jid
and then call chat.send(...)