How to retrieve sender name or its JID in multi user chat message with Smack?
Asked Answered
K

2

6

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

Keffer answered 14/8, 2014 at 11:31 Comment(0)
D
3

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.

Drinker answered 14/8, 2014 at 13:3 Comment(5)
hi i have printed value of that variable and its return me as [email protected] welcome is the name of my group/room and its not return me nick name of user who sent that msgKeffer
Which variable? fromName or nick?Drinker
sorry it was fromName variable. i am getting nick name in nick variable. thank you, can i ask one more doubt i have its not related this question but related to multi user chat?Keffer
Better post that as a separate question, so more people can have a look at it.Drinker
If messages you receive through a MUC do not contain the real sender's JID it maybe that the MUC is configured to be ANONYMOUS. This can be queried by xmpp.org/extensions/xep-0045.html#disco-roominfoClaro
T
0

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)

Tonitonia answered 14/8, 2014 at 12:28 Comment(2)
when i receive msg in my listener code, it just return room's JID as sender. so i am unable to distinguish who is sender of message in group.Keffer
Enable Smack debug and show use the full stanza you a receiving.Tonitonia

© 2022 - 2024 — McMap. All rights reserved.