XMPP Group Chat Android
Asked Answered
R

3

8

I implemented a Group Chat mechanism in my Android where I have created groups and add members through REST API Plugin of Openfire. sending messages to to the same group not delivering messages to all members of the same group. Please see my Error Log for the same, and suggest me any solution regarding the same.

Log:

11-26 17:51:42.364  10035-10035/com.myoneapp.chat V/Cursor data==>>﹕ To User ID==>  onCreateLoader=>terehokerahenge
11-26 17:51:47.018  10035-10654/com.myoneapp.chat I/System.out﹕ 05:51:47 PM SENT (0): <message to='[email protected]' id='362-05' type='groupchat'><body>hi</body><SenderName></SenderName><mediaType>0</mediaType><request xmlns='urn:xmpp:receipts'/></message>
11-26 17:51:47.066  10035-10035/com.myoneapp.chat V/ChatWindow﹕ MESSAGE TYPE==>0
11-26 17:51:47.070  10035-10035/com.myoneapp.chat V/ChatWindow﹕ MESSAGE TYPE==>0
11-26 17:51:47.072  10035-10035/com.myoneapp.chat V/ChatWindow﹕ MESSAGE TYPE==>0
11-26 17:51:48.097  10035-10655/com.myoneapp.chat I/System.out﹕ 05:51:48 PM RECV (0): <message to="[email protected]/chat.spectratech.in" id="362-05" type="error" from="[email protected]"><body>hi</body><SenderName/><mediaType>0</mediaType><request xmlns="urn:xmpp:receipts"/><error code="406" type="modify"><not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></message>
11-26 17:51:48.102  10035-10654/com.myoneapp.chat I/System.out﹕ 05:51:48 PM SENT (0): <message to='[email protected]' id='CGIln-9' type='error'><received xmlns='urn:xmpp:receipts' id='362-05'/></message>

Code:

new Thread(new Runnable() {
                @Override
                public void run() {
                    activity.getmService().xmpp.createMUCGroup(etGroupSubject.getText().toString(), mAppPreferences.getUserName());

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            activity.getmService().xmpp.inViteUserstoGroup(jabberids);
                        }
                    });
                }
            }).start();

public void createMUCGroup(String gJID, String owner){
        mMultiUserChat = getMUChatManager().getMultiUserChat(gJID + "@conference.chat.spectratech.in");
        try {
            mMultiUserChat.create(mAppPreferences.getUserName());

// Get the the room's configuration form
           // org.jivesoftware.smackx.xdata.Form form = mMultiUserChat.getConfigurationForm();
// Create a new form to submit based on the original form


            org.jivesoftware.smackx.xdata.Form form = mMultiUserChat.getConfigurationForm().createAnswerForm();
            form.setAnswer("muc#roomconfig_publicroom", true);
            form.setAnswer("muc#roomconfig_roomname", gJID);
            form.setAnswer("muc#roomconfig_roomowners",gJID);
            form.setAnswer("muc#roomconfig_persistentroom", true);

            mMultiUserChat.sendConfigurationForm(form);
            /*org.jivesoftware.smackx.xdata.Form submitForm = form.createAnswerForm();
// Add default answers to the form to submit
            for (java.util.Iterator fields = (java.util.Iterator) form.getFields(); fields.hasNext(); ) {
                org.jivesoftware.smackx.xdata.FormField field = (org.jivesoftware.smackx.xdata.FormField) fields.next();
                if (!org.jivesoftware.smackx.xdata.FormField.Type.hidden.equals(field.getType()) && field.getVariable() != null) {
                    // Sets the default value as the answer
                    submitForm.setDefaultAnswer(field.getVariable());
                }
            }*/
// Sets the new owner of the room
            /*java.util.List owners = new java.util.ArrayList();
            owners.add(mAppPreferences.getUserName()+"@chat.spectratech.in");
            submitForm.setAnswer("muc#roomconfig_roomowners", owners);
// Send the completed form (with default values) to the server to configure the room
            mMultiUserChat.sendConfigurationForm(submitForm);*/
        }catch (Exception e){
            e.printStackTrace();
        }
    }

public void inViteUserstoGroup(java.util.ArrayList<String> users){
        getMUChatManager().addInvitationListener(MyXMPP.this);
        for (int i = 0; i < users.size(); i++) {
            try {
                mMultiUserChat.invite(users.get(i)+"@chat.spectratech.in", "Meet me in this group.");
            } catch (org.jivesoftware.smack.SmackException.NotConnectedException e) {
                e.printStackTrace();
            }
        }

    }

@Override
    public void invitationReceived(org.jivesoftware.smack.XMPPConnection xmppConnection, org.jivesoftware.smackx.muc.MultiUserChat multiUserChat, String s, String s1, String s2, org.jivesoftware.smack.packet.Message message) {
        try {
            System.out.println("Invitation Received==========================>");
            mMultiUserChat.join(s1);
        } catch (org.jivesoftware.smack.SmackException.NoResponseException e) {
            e.printStackTrace();
        } catch (org.jivesoftware.smack.XMPPException.XMPPErrorException e) {
            e.printStackTrace();
        } catch (org.jivesoftware.smack.SmackException.NotConnectedException e) {
            e.printStackTrace();
        }
    }

It returning Error 406, Not acceptable

Roseboro answered 26/11, 2015 at 12:48 Comment(0)
H
7

I think you are missing the implementation of auto accepting Group chat joining Request in your code.

Below code is working for AMACK group chat using Openfire Server

1. Creating XMPP Connection

    XMPPTCPConnection connection = new XMPPTCPConnection(config);
    connection.connect();
    connection.login(ID1, password1);
    Presence presence = new Presence(Presence.Type.available);
    connection.sendPacket(presence);

2. Creating Persistant Group Chat Room

    MultiUserChat chatRoom = new MultiUserChat(connection, "[email protected]");
    chatRoom.create("nagarjuna");
    Form form = chatRoom.getConfigurationForm().createAnswerForm();
    form.setAnswer("muc#roomconfig_publicroom", true);
    form.setAnswer("muc#roomconfig_roomname", "room786");
    form.setAnswer("muc#roomconfig_roomowners",owners);
    form.setAnswer("muc#roomconfig_persistentroom", true);
    chatRoom.sendConfigurationForm(form);

3. Sending invitation to ride participants

    MultiUserChat.addInvitationListener(connection, groupChatListener);
    chatRoom.invite("surya@dishaserver", "hi surya");

4. Auto accepting the request of RIDER to join group chat

    public class GroupChatListener implements InvitationListener{
    String nickname;

    public GroupChatListener(String nick)
    {
        nickname = nick;
    }

    @Override
    public void invitationReceived(XMPPConnection con, String room,String inviter, String reason, String password, Message message)
    {
        System.out.println(" Entered invitation handler... ");
        try
        {
            MultiUserChat chatRoom = new MultiUserChat(con, room);
            chatRoom.join(nickname);
        }
        catch (NoResponseException | XMPPErrorException| NotConnectedException e)
        {
            e.printStackTrace();
        } catch (SmackException e)
        {
            e.printStackTrace();
        }
        System.out.println(" Invitation Accepted... ");
    }

}

5. Sending message to group chat members

private static void sendMessageToRoomOccupants(XMPPTCPConnection connection) throws NotConnectedException
{
    Message msg = new Message("[email protected]",Message.Type.groupchat);
    msg.setBody("This is nagarjuna friednds. Please join this room and let us have fun."); connection.sendPacket(msg);
}

6. Receiving the group chat message by ride users

MultiUserChat chatRoom = new MultiUserChat(connection, "[email protected]");
chatRoom.addMessageListener(new GroupChatMsgListener());

package com.disha.test;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.packet.Packet;

public class GroupChatMsgListener implements PacketListener
{

    @Override
    public void processPacket(Packet packet) throws NotConnectedException
    {
        System.out.println(" Received group cht messages... ");
        System.out.println("from : "+packet.getFrom());
        System.out.println("to : "+packet.getTo());
        System.out.println(packet.toString());
    }
}
Hawsepipe answered 26/11, 2015 at 13:33 Comment(3)
Hi Mayur, Thanks for your help, but I already tried these codes in my app, but still it is not working. I have edited my question, please check my Code, and tell me where I am going wrong.Roseboro
I also follow the same link you mentioned the above code, sites.google.com/site/nextthoughtlabs/engineering/…Roseboro
in your code, you used object of MUC which you have created group to join in addInvitationListener. that's wrong. Please, use new created object of MultiUserChat as define in 4th step in invitationReceived functionHawsepipe
A
0

In order to send messages in the groupchat you need to join it first:

mMultiUserChat.join("yournickname");
Aida answered 2/12, 2015 at 13:39 Comment(2)
I already implemented the same. Please suggest any error in my code.Roseboro
In your code I see join call only when invite receiving. Invite sender must join tooAida
F
0

Its not working in 4.1.9 version, you can try this one:

    public MultiUserChat mMultiUserChat;
    private MultiUserChatManager mMultiUserChatManager;

    mMultiUserChatManager = MultiUserChatManager.getInstanceFor(mAbstractXMPPConnection);
    mMultiUserChatManager.addInvitationListener(this);

    mMultiUserChat = mMultiUserChatManager.getMultiUserChat(room);
    mMultiUserChat.addMessageListener(this);

    try {
        mMultiUserChat.join(yournickname);

      //  mMultiUserChat.sendConfigurationForm(new Form(DataForm.Type.submit));

    } catch (SmackException.NoResponseException e) {
        e.printStackTrace();
    } catch (XMPPException.XMPPErrorException e) {
        e.printStackTrace();
    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }

and for send message:

     Message msg = new Message(room, Message.Type.groupchat);
     msg.setBody(message);
     mMultiUserChat.sendMessage(msg);

Hope this is helpful, thanks.

Fanciful answered 23/2, 2017 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.