6
votes

i am working on chat app based on XMPP in Android.

i have done one to one chat functionality, but having some problem in multi user chat. i have successfully created new chat room, multiple users can join that room. i have also written code for addPacketListener for Group chat with PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);

i also receiving messages in that listener when user sents message in group, but i am unable to distinguish which user has sent message.

like one to one chat message packet has function message.getFrom() to retrieve senders JID.

in case of multi user chat, same function returns the group/room JID as sender.

i have also tried to set property of Message while sending message.setFrom(senderJID); or message.setFrom([email protected]/Groupname);

still i am unable to get senderJID or its nickname.

so my question is: how to get sender user JID or its nickname? from message(packet) in messageListener

Code for sending msg in group is:-

String to = strGroupJID;
String text = etChatOnTextBox.getText().toString();
if(!text.equals(""))
{
        Message msg = new Message(to, Message.Type.groupchat);
        msg.setBody(text);
        String name1 = xmppConnection.getUser();
        name1 = name1.substring(0, name1.lastIndexOf("@"));
        name1 = name1 + "@conference.192.168.56.1";
       // name1 = name1 + "@conference.192.168.56.1/" + strGroupName ;
        msg.setFrom(name1);
        muc.sendMessage(msg);
 }

Code for Receive Message is:-

PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
        connection.addPacketListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());

                    Log.i("ChatOn", "Text Recieved " + message.getBody()
                            + " from " + fromName );
           }
        }
   });

any help or suggestion is appreciated thank you

2

2 Answers

3
votes

This piece of your code will identify the chat room:

String fromName = StringUtils.parseBareAddress(message
        .getFrom());

You can use this code to identify the nick of the chat room user:

String nick = StringUtils.parseResource(message
        .getFrom());

This is because JIDs of multi user chat messages look like roomname@server/nickname, with the nickname of the user being the resource of the JID.

0
votes

How to get sender user JID or its nickname? from message(packet) in messageListener

If you look at XEP-45 7.4 you will see that the from JID is the bare JID of the MUC plus the MUC's member nickname as resource. So the nickname is the resource of the from JID.

If the room is non-anonymous the you can get the full JID of the occupant in the extended presence information (XEP-45 7.2.4)