0

I am working on a chatting application in which i can send messages , images , videos etc i have done this in one to one chat also fulfill it in group chat . But the issue is :- I have to always join the each group everytime i login otherwise i am not been able to recieve message from different groups .

here is the way i join group each time .

  MultiUserChat muc= new  MultiUserChat(mConnection,"hsjsmqb@conference.11.111.111.111");
    String userNAme ="222222222";
muc.join(userNAme);

if i do not join group everytime i do not recieve messages . if i join group i started recieving messages .

My Question is is this the only solution or all group chatting works this way. or am i doing some thing wrong
i googled but did not find any solution . if it is duplicate question or any answer related to my question please share the link thanks

This is the code :-

public boolean createChatRoom() {
        String name = edtGroupName.getText().toString();
        if (!(connection.isConnected() && name.length() != 0)) {
            return false;
        }
        try {
            // Create a MultiUserChat

            String userName = Utils.covertIntoSubString(connection.getUser(), Constant.AT);
            roomName = (name + md5String(getDateTime()) + userName + Constant.CONFERENCE + connection.getServiceName()).replaceAll(" ", "");
            MultiUserChat muc = new MultiUserChat(connection, roomName);

            // Create a chat room
            muc.create(roomName);
            // set Room Name as room subject
            muc.changeSubject(name);// RoomName room name


            // To obtain the chat room configuration form
            Form form = muc.getConfigurationForm();
            // Create a new form to submit the original form according to the.
            Form submitForm = form.createAnswerForm();
            // To submit the form to add a default reply
            for (Iterator<FormField> fields = form.getFields(); fields
                    .hasNext(); ) {
                FormField field = (FormField) fields.next();
                if (!FormField.TYPE_HIDDEN.equals(field.getType())
                        && field.getVariable() != null) {
                    // Set default values for an answer
                    submitForm.setDefaultAnswer(field.getVariable());
                }
            }

            // Set the chat room of the new owner
            List<String> owners = new ArrayList<String>();
            owners.add(connection.getUser());// The user JID
//            submitForm.setAnswer("muc#roomconfig_roomowners", owners);
            // Set the chat room is a long chat room, soon to be preserved
            submitForm.setAnswer("muc#roomconfig_persistentroom", true);
            // chat room is public
            submitForm.setAnswer("muc#roomconfig_publicroom", true);
            // Allows the user to modify the nickname
            submitForm.setAnswer("x-muc#roomconfig_canchangenick", true);
            // Allows the possessor to invite others
//            submitForm.setAnswer("muc#roomconfig_allowinvites", true);

//            submitForm.setAnswer("muc#roomconfig_enablelogging", true);
            // Only allow registered nickname log
//            submitForm.setAnswer("x-muc#roomconfig_reservednick", true);

            // Allows the user to register the room
//            submitForm.setAnswer("x-muc#roomconfig_registration", true);

            muc.sendConfigurationForm(submitForm);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
public void inviteFriends(String userJid) {
    try {
        String groupName = edtGroupName.getText().toString();
        Message msg = new Message();
        msg.setBody(groupName);
        MultiUserChat muc = new MultiUserChat(connection, roomName);
        if (muc != null) {
            muc.grantMembership(userJid);
            muc.invite(msg, userJid, groupName);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void invitationrecvd(){
MultiUserChat chatRoom = new MultiUserChat(con, rum);
                    try {
                        chatRoom.join(userName);
                        saveGroupsToDb(userName + Constant.AT + Constant.HOST + Constant.SLASHSMACK, rum, group);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

}

And This is the group message Listener on home screen

PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
        groupMessagesListeners = new GroupMessagesListeners();
        mConnection.addPacketListener(groupMessagesListeners,filter);
Ali Abhas
  • 107
  • 7

2 Answers2

1

The groupchat is addressed to a XMPP muc (multi user chat) so you would need to join the muc in order to receive messages sent in that particular group. You can read more about this in https://xmpp.org/extensions/xep-0045.html.

Here is an excerpt from the link:

7.2.1 Groupchat 1.0 Protocol

In order to participate in the discussions held in a multi-user chat room, a user MUST first become an occupant by entering the room. In the old groupchat 1.0 protocol, this was done by sending presence with no 'type' attribute to , where "room" is the room ID, "service" is the hostname of the chat service, and "nick" is the user's desired nickname within the room:

manishg
  • 9,520
  • 1
  • 16
  • 19
  • The issue is i have to rejoin the already joined group to recieve messages ... Elsewise i am not been able to recieve messages against group i am a member of ... – Ali Abhas Feb 20 '17 at 19:56
  • Do you leave the group after joining? May be send presence every 10 second or so that you are still part of the room – manishg Feb 20 '17 at 20:00
  • No i do not leave the group I am a part of group but donot recieve messages... I dont know whats wrong ... – Ali Abhas Feb 20 '17 at 20:25
  • Post a log. If you the join the muc and don't leave, you will get a muc message. So either there is something wrong with the server or your code or the xmpp library which you are using. A simple check would be to find out whether you get the presence from other participants when they join the muc. – manishg Feb 20 '17 at 20:27
  • So you are saying we only have to join group only one not everytime . is it correct.. – Ali Abhas Feb 20 '17 at 20:32
  • Could you please share a code link so i can get some improve code...... I am also trying share my log with you ... – Ali Abhas Feb 20 '17 at 20:34
  • If you the join the muc, you have to do it only once. You can update your presence if it changes or someone can boot you out of the room if they have access to do the same. If someone boots you out, you will get a message. It would be better if you post your code so that we can check what's going on. You will find sample examples online. Here is an example http://androidbydavid.blogspot.com/2013/07/multiuserchat-with-xmpp-android-tutorial.html?m=1 – manishg Feb 20 '17 at 20:39
  • I am the admin of group... So now i have to update my presence against each joined group .... I will share code with you too .. – Ali Abhas Feb 20 '17 at 20:45
  • i have added the code in my question you can see .please guide . Thanks – Ali Abhas Feb 21 '17 at 10:20
  • Quick comment: you need to make sure your muc variable is persistent in order to receive messages asynchronously. Also you said you are part of multiple groups so you need to create a hashmaps of muc variables – manishg Feb 21 '17 at 12:49
  • yes it is persistent group ... as shown while creating group form ... and also you where talking about sending presence is also like joining a room again ... beacuse when you explore the join function of multiuserchat they are also sending available on joining and unavailable presence on leave... – Ali Abhas Feb 21 '17 at 12:56
0

More or less I already had answered this kind of answer, but your code it's much cleaner (Can't able to receive group chat messages using smack-android:4.1.4).

1) When you create a multiuserchat, to finalize it you must JOIN this chat or the room it's just configurated but not still active.

muc.sendConfigurationForm(submitForm);
muc.join("My Nickname","password"); //omit password if not needed

2) To automatically join your groupchats, you can use PubSub feature A snippet with Smack can looks like:

BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(mConnection);
bookmarkManager.addBookmarkedConference
        ("My roomname label",
        roomName,
        true,
        "My Nickname",
        password);

add this code when:

  • you create a groupchat
  • you accept an groupchat's invitation.

(To remove a bookmark, just:

this.bookmarkManager.removeBookmarkedConference(roomName)

)

Finally, after login, add a method to autojoin the groupchat:

List<BookmarkedConference> list = BookmarkManager.getBookmarkManager(mConnection).getBookmarkedConferences();

        MultiUserChat muc;
        for (BookmarkedConference conference : list)
        {
            System.out.println("- Conference with bookmark: " + conference.getName() +
                    " and jid: " + conference.getJid());


            if ( (muc = multiUserChatManager.getMultiUserChat(conference.getJid()) )  != null
            && conference.isAutoJoin())
                {

                    muc.join("My Nickname");
                        //foo
    }
}

After this you have all to config and manage your groupchats. Personally I don't like to add to mConnection a generic PacketListener due to the consequent difficoulty to synchronyze messages receveid with front end, but this will be eventually another branch.

Community
  • 1
  • 1
MrPk
  • 2,862
  • 2
  • 20
  • 26
  • Thanks friend for detail looking into the code . but the thing you shared is to always join the room we created ... and my question is is there any way we dont need to join room everytime we are already member of . Or is it the only solution .??? Thanks @MrPk – Ali Abhas Feb 21 '17 at 12:09
  • Only solution. To be a member (owner, admin, member, outcast) just gives your RIGHTS to do something (like change subject and so on) but in theory an admin user can be added as owner as default and of course he doesn't need to receive any message. Basically you receive the messages from Time TJ (join) up to Time TL (left), others config are just configs. – MrPk Feb 21 '17 at 13:18
  • so my understanding is i have to join all groups everytime i launch the application . so after that i started receiving messages against groups from the time of joining and leaving . – Ali Abhas Feb 21 '17 at 16:45
  • exactly @AliAbhas (add with edit: with Presence it's possible to set a status like "do not disturb" and interrupt to receive messages WHILE you already joined a chat) – MrPk Feb 21 '17 at 16:46