1
votes

Problem Description

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");

Question

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?

3
In the line Chat chat = ChatManager.getInstanceFor(connection).createChat(jid);, how do you get the jid - Simran Sharma

3 Answers

2
votes

There is an example with an older version of smack:

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

public class Test {
    public static void main(String args[]) throws XMPPException {
        ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1", 5222);    
        XMPPConnection connection = new XMPPConnection(config); 
        connection.connect();   
        connection.login("userx", "123456");
        ChatManager cm = connection.getChatManager(); 
        Chat chat = cm.createChat("tongqian@tsw-PC", null);
        /*
         * add listener
         */
        cm.addChatListener(new ChatManagerListener() {
            @Override
            public void chatCreated(Chat chat, boolean create) {
                chat.addMessageListener(new MessageListener() {
                    @Override
                    public void processMessage(Chat chat, Message msg) {
                        System.out.println(chat.getParticipant() + ":" + msg.getBody());
                    }
                });
            }
        });
        chat.sendMessage("hello");  
        while(true);        
        //connection.disconnect();  
    }
}
0
votes

You can refer to this code snippet:

public void sendMessage(String to, Message newMessage) {
if(chatManager!=null) {
Chat newChat = chatManager.createChat(to);

try {
if (connection.isConnected() && connection.isAuthenticated()) {

newChat.sendMessage(newMessage);
}
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
else
{
Log.d(TAG,”chatmanager is null”);
}
}

And the link is https://ramzandroidarchive.wordpress.com/2016/03/13/send-messages-over-xmpp-using-smack-4-1/ .

0
votes

Answer

Digging a bit deeper I found the answer, the code below will help to send a message

Sending Message Code

final Chat chat = ChatManager.getInstanceFor(connection).chatWith(jid);

Message newMessage = new Message(jid, Message.Type.chat);
newMessage.setBody(message);

chat.send(newMessage);

Conclusion

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(...)