未接收多用户聊天Xmpp samck android的消息
我正在开发一个聊天应用程序,在其中我可以发送消息、图像、视频等 我已经在一对一聊天中完成了这一点,也在群聊中实现了这一点。 但问题是:- 每次登录时我都必须加入每个组,否则我无法接收来自不同组的消息 下面是我每次加入小组的方式未接收多用户聊天Xmpp samck android的消息,android,xmpp,smack,multiuserchat,Android,Xmpp,Smack,Multiuserchat,我正在开发一个聊天应用程序,在其中我可以发送消息、图像、视频等 我已经在一对一聊天中完成了这一点,也在群聊中实现了这一点。 但问题是:- 每次登录时我都必须加入每个组,否则我无法接收来自不同组的消息 下面是我每次加入小组的方式 MultiUserChat muc= new MultiUserChat(mConnection,"hsjsmqb@conference.11.111.111.111"); String userNAme ="222222222"; muc.join(use
MultiUserChat muc= new MultiUserChat(mConnection,"hsjsmqb@conference.11.111.111.111");
String userNAme ="222222222";
muc.join(userNAme);
如果每次我没有加入组,我就不会收到消息。
如果我加入这个组,我就开始接收消息
我的问题是,这是唯一的解决办法还是所有的集体聊天都是这样。
还是我做错了什么事我在谷歌上搜索,但没有找到任何解决办法。 如果是重复的问题或任何与我的问题相关的答案,请共享链接 谢谢 代码如下:-
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();
}
}
groupchat的地址是XMPP muc(多用户聊天),因此您需要加入muc才能接收在该特定组中发送的消息。您可以在中阅读更多关于此的信息 以下是该链接的摘录: 7.2.1群聊1.0协议 以参与多用户聊天中的讨论 房间,用户必须首先通过进入房间成为占用者。在里面 在旧的groupchat 1.0协议中,这是通过发送状态信息来实现的 没有“type”属性,其中“room”是房间 ID,“service”是聊天服务的主机名,“nick”是 房间内用户所需的昵称:
或多或少我已经回答了这种问题,但是你的代码更简洁() 1) 当您创建多用户聊天时,要完成它,您必须加入此聊天室或刚刚配置但尚未激活的聊天室
muc.sendConfigurationForm(submitForm);
muc.join("My Nickname","password"); //omit password if not needed
2) 要自动加入群组聊天,您可以使用PubSub功能
带有Smack can的代码段如下所示:
BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(mConnection);
bookmarkManager.addBookmarkedConference
("My roomname label",
roomName,
true,
"My Nickname",
password);
在以下情况下添加此代码:
- 你创建了一个群组聊天
- 您接受groupchat的邀请
this.bookmarkManager.removeBookmarkedConference(roomName)
)
最后,登录后,添加自动加入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
}
}
List List=BookmarkManager.getBookmarkManager(mConnection.getBookmarkedConferences();
多用户聊天;
用于(书签会议:列表)
{
System.out.println(“-Conference with bookmark:”+Conference.getName()+
和jid:“+conference.getJid());
if((muc=multiUserChatManager.getMultiUserChat(conference.getJid())!=null
&&conference.isAutoJoin())
{
muc.join(“我的昵称”);
//福
}
}
在此之后,您必须配置和管理您的GroupChat。就我个人而言,我不喜欢在
mConnection
中添加一个通用的PacketListener
,因为这样会导致难以与前端同步接收到的消息,但这最终将是另一个分支。问题是我必须重新加入已加入的组以接收消息。。。否则,我无法收到针对我所属的组的邮件…您加入后是否离开该组?可能每10秒左右发送一次状态信息,这样您仍然是房间的一部分不,我不离开团队,我是团队的一部分,但不接收消息。。。我不知道出了什么问题…发日志。如果你加入muc并且不离开,你会收到muc的消息。因此,可能是服务器、代码或您正在使用的xmpp库有问题。一个简单的检查是,当其他参与者加入muc时,你是否能从他们那里得到出席。所以你是说我们只需要加入一个小组,而不是每次都要。这是正确的..感谢朋友详细查看代码。但你分享的是永远加入我们创建的房间。。。我的问题是,我们有没有办法不必每次都加入这个房间,因为我们已经是其中的一员了。还是这是唯一的解决办法。???谢谢@MrPkOnly解决方案。成为会员(所有者、管理员、会员、弃儿)只会赋予你做某事的权利(如更改主题等),但理论上,管理员用户可以作为默认用户添加为所有者,当然他不需要收到任何消息。基本上,您会收到从时间TJ(join)到时间TL(left)的消息,其他配置只是配置。因此,我的理解是,每次启动应用程序时,我都必须加入所有组。因此,从加入和离开时起,我就开始接收针对群组的消息。确切地说是@AliAbhas(通过编辑添加:通过状态,可以设置“请勿打扰”和中断等状态,以便在您已经加入聊天时接收消息)
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
}
}