Asad AndyDev Asad AndyDev - 3 months ago 59
Android Question

How to send one to one message using smack API 4.1.0 android

I am using the code from the official documentation of smack API to send message to a specific Jabber ID.

CLick Here

I am able to receive messages from a room using below code.

public void joinChatRoom(){
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
multiUserChat = manager.getMultiUserChat("test@-mbp-9");
try {
multiUserChat.join("user");
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
ChatManager.getInstanceFor(connection).addChatListener(new ChatManagerListener() {
@Override
public void chatCreated(Chat chat, boolean createdLocally) {
chat.addMessageListener(new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
System.out.println(message.getBody());
}
});
}
});
System.out.println("Test");
}


My Question is that how can I send message to a specific JID because i am not able to work it out even after a lot of googling what i am missing. Connection is fine user is also authenticating but below code is now working for send message.

public void sendMsg() {
if (connection.isConnected()) {
// Assume we've created an XMPPConnection name "connection"._
chatmanager = ChatManager.getInstanceFor(connection);
newChat = chatmanager.createChat("user123@csofts-mbp-9", new ChatMessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
System.out.println("Received Message:"+message);
}
});

try {
System.out.println("check the message....");
newChat.sendMessage("Howdy!alksd;lsakdsa;lkdsa;lksa;lsa");
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
}


Any Help will be appreciated Thanks.

Answer

Here is example of sending message code from my last project:

private void sendMessage(String body, String toJid) {
     try {
         Jid jid = JidCreate.from(toJid + "@" + MyApplication.CHAT_DOMAIN);
         Chat chat = ChatManager.getInstanceFor(mConnection)
                 .createChat(jid.asJidWithLocalpartIfPossible());
         chat.sendMessage(body);
     } catch (Exception e) {
     } 
}

UPDATE:

How to receive a message:

When you are authenticated:

ChatManager.getInstanceFor(mConnection).addChatListener(YourConnectionClass.this);

(YourConnectionClass should implement ChatManagerListener and ChatMessageListener)

Also you need to implement the methods below:

@Override
public void chatCreated(Chat chat, boolean b) {
    chat.addMessageListener(ChatConnection.this);
}

@Override
public void processMessage(Chat chat, Message message) {
    if (message.getType().equals(Message.Type.chat) || message.getType().equals(Message.Type.normal)) {
        if (message.getBody() != null) {
            Jid jid = message.getFrom();
            Localpart localpart = jid.getLocalpartOrNull();
            ...
        }
    }
}
Comments