如何在Android中使用smack 4.1创建XMPP群聊并添加成员 [英] How to create xmpp group chat and add members using smack 4.1 in android

查看:397
本文介绍了如何在Android中使用smack 4.1创建XMPP群聊并添加成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我想使用smack 4.1创建组并将用户添加到组中。
我可以创建组,但无法在该组中添加用户。
创建组的我的代码是...

In my app, I want to create groups and add users to the groups using smack 4.1. I am able to create group but unable to add user in that groups. My Code for create groups is...

创建组

if (connection != null) {
        String groupName = XmppUtils.buildMUCName(grpName);
        MultiUserChat multiUserChat = managerMuc.getMultiUserChat(groupName);
        if (!multiUserChat.isJoined()) {
            boolean createNow;
            try {
                multiUserChat.createOrJoin(grpName);
                createNow = true;
                if (createNow) {
                    Form form = multiUserChat.getConfigurationForm().createAnswerForm();
                    form.setAnswer("muc#roomconfig_publicroom", true);
                    form.setAnswer("muc#roomconfig_roomname", grpName); //                        form.setAnswer("muc#roomconfig_roomowners", userDate.getUserId().toString() + "@" + Config.ChatValues.SERVICE_NAME);
                    form.setAnswer("muc#roomconfig_persistentroom", true);
                    List<String> cast_values = new ArrayList<String>();
                    cast_values.add("moderator");
                    cast_values.add("participant");
                    cast_values.add("visitor");
                    form.setAnswer("muc#roomconfig_presencebroadcast", cast_values);
                    multiUserChat.sendConfigurationForm(form);
                    multiUserChat.join(userDate.getUserId().toString() + "@" + Config.ChatValues.SERVICE_NAME); //                        multiUserChat.sendConfigurationForm(new Form(DataForm.Type.submit)); //this is to create the room immediately after join.
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Log.e("createGroup", "=======multiUserChat.isJoined()=====>");
        }
    }

添加用户的代码
这就是我要添加用户的目的

  // Create a MultiUserChat using an XMPPConnection for a room

   String roomName = groupName + "@" + Config.ChatValues.GROUP_SERVICE_NAME;
    MultiUserChat muc2 = managerMuc.getMultiUserChat(roomName);
    // User2 joins the new room
    // The room service will decide the amount of history to send
    // The number of chat room services will decide to accept the historical record
   /* DiscussionHistory history = new DiscussionHistory();
    history.setMaxStanzas(0);*/

    //history.setSince(new Date());
    try {

        for (int i = 0; i < selectedLisrArray.size(); i++) {
            String userId = selectedLisrArray.get(i).userId.toString() + "@" + Config.ChatValues.SERVICE_NAME;
            Log.e("joinGroups", "=========>" + roomName + " users to join  " + userId);

            muc2.invite(msg, userId, "let's  join this room ");
            muc2.sendMessage(userId + " : You have joined the group : " + roomName);
        }

    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }


推荐答案

我做的一点点相同只需发送邀请,然后在openfire上安装插件Subscription(如果使用的话)。更改设置以接受任何请求。

I have done little bit same.Just you send invitation and then install plugin Subscription on openfire if you used.Change settings to accept any request.

如果您不使用openfire,则只需接受他们的邀请即可另一边。
希望能为您提供帮助。

If you not using openfire then you just accept their invitation on other side. Hope this will help you.

MultiUserChat muc = new MultiUserChat(XMPP.getInstance().getConnection(getActivity()), groupname);
                try {
                    muc.create(StringUtils.parseName(XMPP.getInstance().getConnection(getActivity()).getUser()));
                    muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
                } catch (XMPPException e) {
                } catch (NoResponseException e) {
                    e.printStackTrace();
                } catch (SmackException e) {
                    e.printStackTrace();
                }

                for (int i = 0; i < alSelectedContacts.size(); i++) {

                    Log.e("tag", "group chating purpose1 ::" + alSelectedContacts.get(i).get("id"));
                    try {
                        muc.invite((alSelectedContacts.get(i).get("id") + "_user") + "@" + XMPP.HOST,
                                alSelectedContacts.get(i).get("id") + "_user");
                    } catch (NotConnectedException e) {
                        e.printStackTrace();
                    }

                }

                try {
                    muc.sendMessage("New group created");
                } catch (NotConnectedException e1) {
                    e1.printStackTrace();
                } catch (XMPPException e1) {
                    e1.printStackTrace();
                }

在此处获取Muc邀请,

MultiUserChat.addInvitationListener(mXmppConnection,
        new InvitationListener() {

            @Override
            public void invitationReceived(Connection connection,
                    String room, String inviter, String reason,
                    String unKnown, Message message) {

                //MultiUserChat.decline(mXmppConnection, room, inviter,
                    //  "Don't bother me right now");
                // MultiUserChat.decline(mXmppConnection, room, inviter,
                // "Don't bother me right now");
                try {
                   muc.join("test-nick-name");
                   Log.e("abc","join room successfully");
                   muc.sendMessage("I joined this room!! Bravo!!");
                } catch (XMPPException e) {
                   e.printStackTrace();
                   Log.e("abc","join room failed!");
                }
            }
        });

这篇关于如何在Android中使用smack 4.1创建XMPP群聊并添加成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆