我正在开发一个聊天应用程序,我可以在其中发送消息,图像,视频等,我已经在一对一聊天中完成了这一点,也可以在群聊中实现它。但问题是:-我必须每次登录时都加入每个组,否则我无法收到来自不同组的消息。
这是我每次加入小组的方式。
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);
groupchat 寻址到 XMPP muc(多用户聊天),因此您需要加入 muc 才能接收在该特定组中发送的消息。您可以在https://xmpp.org/extensions/xep-0045.html中阅读有关此内容的更多信息。
这是链接的摘录:
7.2.1 Groupchat 1.0 协议
为了参与在多用户聊天室中进行的讨论,用户必须首先通过进入房间而成为居住者。在旧的 groupchat 1.0 协议中,这是通过将没有“type”属性的存在发送到来完成的,其中“room”是房间 ID,“service”是的主机名,“nick”是用户在房间内所需的昵称:
或多或少我已经回答了这种答案,但你的代码更干净(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 needed
2)要自动加入您的群聊,您可以使用PubSub功能带有 Smack 的代码段可以如下所示:
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
}
}
在这之后,你必须配置和管理你的群聊。我个人不喜欢添加到mConnection
一个通用的PacketListener
由于随之而来的困难与前端同步消息 receveid,但这最终将是另一个分支。
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(2条)