我正在开发一个聊天应用程序,在其中我可以发送消息,图像,视频等,我已经做到了这一点,在一对一聊天,也在群聊中实现它。但问题是:-我必须总是加入每个组,每次我登录,否则我不能收到来自不同组的消息。
下面是我每次加入群的方式。
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();
}
}这是主屏幕上的群组消息监听器
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
groupMessagesListeners = new GroupMessagesListeners();
mConnection.addPacketListener(groupMessagesListeners,filter);发布于 2017-02-21 03:51:31
群聊寻址到XMPP muc (多用户聊天),因此您需要加入muc才能接收在该特定群组中发送的消息。你可以在https://xmpp.org/extensions/xep-0045.html上阅读更多关于这方面的内容。
以下是链接的摘录:
7.2.1群聊1.0协议
要参与多用户聊天室中的讨论,用户必须首先通过进入聊天室成为居住者。在旧的groupchat 1.0协议中,这是通过向发送没有‘类型’属性的在线状态来完成的,其中“房间”是房间ID,“服务”是聊天服务的主机名,"nick“是用户在房间内所需的昵称:
发布于 2017-02-21 19:40:55
或多或少我已经回答过这种问题了,但是你的代码要干净得多(Can't able to receive group chat messages using smack-android:4.1.4)。
1)当你创建了一个多用户聊天,要完成它,你必须加入这个聊天室,或者加入这个聊天室,这个聊天室刚刚配置好,但还没有激活。
muc.sendConfigurationForm(submitForm);
muc.join("My Nickname","password"); //omit password if not needed2)要自动加入群聊,您可以使用PubSub功能带有Smack的代码片段可以如下所示:
BookmarkManager bookmarkManager = BookmarkManager.getBookmarkManager(mConnection);
bookmarkManager.addBookmarkedConference
("My roomname label",
roomName,
true,
"My Nickname",
password);在以下情况下添加此代码:
(要删除书签,只需:
this.bookmarkManager.removeBookmarkedConference(roomName))
最后,登录后,添加一个自动加入群聊的方法:
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
}
}在此之后,您必须配置和管理您的群聊。就我个人而言,我不喜欢在mConnection中添加一个通用的PacketListener,因为这样很难将消息receveid与前端同步,但这最终将是另一个分支。
https://stackoverflow.com/questions/42349101
复制相似问题