XMPP Room Invitation

前提是你 提交于 2019-12-21 21:38:46

问题


In my chatting application I want to implement Group Chatting functionality. For the same I want to create rooms and send the invitations to my friends to join the room. Here is my code to join and invite the friend to room.

To Create the Room

//Create Room
    btn_CreateRoom = (Button)findViewById(R.id.btn_usermenu_CreateRoom);
    btn_CreateRoom.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            try {
                muc = new MultiUserChat(connection, "room1@conference.abc.com");

                muc.join("Sunil","123456");
                muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));

            } catch (XMPPException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Room Created");
        }
    });


    btn_Invite = (Button)findViewById(R.id.btn_usermenu_InviteToRoom);
    btn_Invite.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            muc.invite("sunil@abc.com", "Please join this room");



        }
    });

}

To recieve the invitation I have implemented an Invitation Listener in my Service Class. But I am unable to receive the invitaion via notification. Wats the problem with the code.

Here is my Invitation Listener.

MultiUserChat.addInvitationListener(connection, new InvitationListener() {

            @Override
            public void invitationReceived(Connection arg0, String arg1, String arg2,
                    String arg3, String arg4, Message arg5) {
                // TODO Auto-generated method stub

                System.out.println("Received??");
                notification("Invitation Received");

Please let me know Why I am not receiving the invitation.??

Thanks


回答1:


you write this code after xmppconnection.connect()

MultiUserChatManager.getInstanceFor(connection).addInvitationListener(new InvitationListener() {
                    @Override
                    public void invitationReceived(XMPPConnection conn, MultiUserChat room, String inviter, String reason, String password, Message message) {
                        MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(conn);
                        try {
                            room.join("pandian");
                        } catch (SmackException.NoResponseException e) {
                            e.printStackTrace();
                        } catch (XMPPException.XMPPErrorException e) {
                            e.printStackTrace();
                        } catch (SmackException.NotConnectedException e) {
                            e.printStackTrace();
                        }
                    }


                });

its work for me ..try it




回答2:


You can try this

public class myclass extends Activity implements InvitationListener{
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                MultiUserChat.addInvitationListener(Connection,myclass.this);
}
@Override
    public void invitationReceived(Connection conn, final String room,
            String inviter, String reason, String password, Message message) {

        Log.e("inviter", inviter);
        Log.e("message", message.getBody());
        Log.e("reason", reason);
        Log.e("room", room);

    }
}

This might solve your problem.




回答3:


We missed Thread here:

Just add below code to join group:

private void setChatRoomInvitationListener() {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                MultiUserChat.addInvitationListener(xmppConnection, new InvitationListener() {
                    @Override
                    public void invitationReceived(Connection connection,String room, String inviter, String reason,String unKnown, Message message) {
                        XMPPConnectionUtils.configureChatStandards();
                        MultiUserChat muc = new MultiUserChat(connection, room);
                        try {
                            muc.join("My_Name_Here");
                        } catch (XMPPException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
        thread.start();
    }

Hope this will help a lot.



来源:https://stackoverflow.com/questions/13028384/xmpp-room-invitation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!