0
votes

I am new in android and i tried to create chat application using smack api and ejabberd. i implement one to one chat and its work good. but i not able to receive chatroom message. how can i do?

here is xmpp.java

public class MyXMPP {
        public MyXMPP(MyService context, String serverAdress, String logiUser,
                      String passwordser) {
            this.serverAddress = serverAdress;
            this.loginUser = logiUser;
            this.passwordUser = passwordser;
            this.context = context;
            this.applicationContext = context;
            init();
        }
        public static MyXMPP getInstance(MyService context, String server,String user, String pass) {
            if (instance == null) {
                instance = new MyXMPP(context, server, user, pass);
                instanceCreated = true;
            }
            return instance;
        }
        public org.jivesoftware.smack.chat.Chat Mychat;
        ChatManagerListenerImpl mChatManagerListener;
        MMessageListener mMessageListener;
        static {
            try {
                Class.forName("org.jivesoftware.smack.ReconnectionManager");
            } catch (ClassNotFoundException ex) {
                // problem loading reconnection manager
            }
        }
        public void init() {
            gson = new Gson();
            mMessageListener = new MMessageListener(context);
            mChatManagerListener = new ChatManagerListenerImpl();
            initialiseConnection();

        }
        private void initialiseConnection() {
            XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
                    .builder();
            config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            config.setServiceName(serverAddress);
            config.setHost("192.168.0.101");
            config.setPort(5222);
            config.setDebuggerEnabled(true);
            XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
            XMPPTCPConnection.setUseStreamManagementDefault(true);
            connection = new XMPPTCPConnection(config.build());
            XMPPConnectionListener connectionListener = new XMPPConnectionListener();
            connection.addConnectionListener(connectionListener);
        }
        private class ChatManagerListenerImpl implements ChatManagerListener {
            @Override
            public void chatCreated(final org.jivesoftware.smack.chat.Chat chat,
                final boolean createdLocally) {
                if (!createdLocally)
                    chat.addMessageListener(mMessageListener);

            }

        }
        public void sendMessage(ChatMessage chatMessage) {
            String body = gson.toJson(chatMessage);
            Mychat = ChatManager.getInstanceFor(connection).createChat(
                    chatMessage.receiver + "@"
                            + context.getString(R.string.server),
                    mMessageListener);
            chat_created = true;
            final Message message = new Message();
            message.setBody(body);
            message.setStanzaId(chatMessage.msgid);
            message.setTo(chatMessage.receiver);
            message.setFrom(chatMessage.sender);
            message.setType(Message.Type.chat);
            Mychat.sendMessage(chatMessage.body);
        }

        public void sendGroupMessage(ChatMessage chatMessage) {
            MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
            MultiUserChat multiUserChat = manager.getMultiUserChat(chatMessage.receiver);
            try {
                multiUserChat.join(chatMessage.sender,"12345");
    //            multiUserChat.addMessageListener((MessageListener) mMessageListener);
            } catch (XMPPException.XMPPErrorException e) {
                e.printStackTrace();
            } catch (SmackException e) {
                e.printStackTrace();
            }
            Message message = new Message(chatMessage.receiver, Message.Type.groupchat);
            message.setBody(chatMessage.body);
            message.setType(Message.Type.groupchat);
            message.setFrom(chatMessage.sender);
            message.setTo(chatMessage.receiver);
            multiUserChat.sendMessage(message);
        }

        /* Its work good for one to one chat */
        private class MMessageListener implements ChatMessageListener {
            Context context;
            public MMessageListener(Context c) {
                this.context = c;
            }
            @Override
            public void processMessage(final org.jivesoftware.smack.chat.Chat chat,
                final Message message) {
                if (message.getType() == Message.Type.chat
                        && message.getBody() != null) {
                    Log.d("Message received",message.getBody);
                }
            }
        }
    }

i implement all the connection xmpp connection in xmpp.java sendGroupMessage() is used to send chat room message and its work good. the problem is receive chat room message.

i also receive message into android log here is the imageenter image description here

1
means you are able to send messages and not receive them in a 1:1 chat?Sarthak Mittal
Yes @SarthakMittalKaushik Makwana
Ok, then how are you able to validate that the message was successfully sent?Sarthak Mittal
Smack will return the status of sent message like sent or errorKaushik Makwana

1 Answers

1
votes

The Chat class that you are using is deprecated! I recommend that you update your smack library to the latest version (4.2 presently).

Here is how you can attach an incoming message listener in Smack-4.2

XMPPTCPConnection connection;  // Assuming you have the connection object

org.jivesoftware.smack.chat2.ChatManager chatManager = org.jivesoftware.smack.chat2.ChatManager.getInstanceFor(connection);
chatManager.addIncomingListener(new IncomingChatMessageListener()
{
    @Override
    public void newIncomingMessage(EntityBareJid from, Message message, org.jivesoftware.smack.chat2.Chat chat)
    {
         // Message received here
    }
});