Can';无法使用smack android:4.1.4接收群聊消息
我正在使用smack库开发聊天应用程序。我在群聊中遇到了一个问题。在我的应用程序中,我正在创建一个组,其中的成员是自动加入的。我希望在组中发送消息时通知所有用户,即使他们没有发起聊天。我的代码如下:我将侦听器置于init方法中,但无法接收消息Can';无法使用smack android:4.1.4接收群聊消息,android,xmpp,smack,groupchat,Android,Xmpp,Smack,Groupchat,我正在使用smack库开发聊天应用程序。我在群聊中遇到了一个问题。在我的应用程序中,我正在创建一个组,其中的成员是自动加入的。我希望在组中发送消息时通知所有用户,即使他们没有发起聊天。我的代码如下:我将侦听器置于init方法中,但无法接收消息 multiUserChatManager = MultiUserChatManager.getInstanceFor(mConnection); mMultiUserChat = multiUserChatManager.g
multiUserChatManager = MultiUserChatManager.getInstanceFor(mConnection);
mMultiUserChat = multiUserChatManager.getMultiUserChat(to);
mConnection.addAsyncStanzaListener(this, null);
DiscussionHistory history = new DiscussionHistory();
history.setMaxStanzas(0);
mMultiUserChat.addMessageListener(this);
mConnection.addSyncStanzaListener(this, null);
try {
mMultiUserChat.join(from, "", history, SmackConfiguration.getDefaultPacketReplyTimeout());
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
这是组的消息侦听器
@Override
public void processMessage(Message message) {
Logg.e(TAG,"Message received group..");
}
我不知道为什么当有人在组中发送消息时,这个方法不调用,即使我加入了组,如果我创建了1个组并加入了2个用户,当1个用户在组中发送消息时,user2就不能接收消息,但是当user2在这个组中发送消息时,他们都可以接收消息
请帮帮我,我找不到解决办法。请不要给出已经不推荐的建议
提前感谢 在完整的代码审查之后,我正在完整地编辑答案-再次-
我建议重构您的代码,以便在多个大型类中保持角色分离
基本上,由于代码中有许多“addasync-addsync”,所以您在错误的侦听器中观察消息,并且您能够接收消息,就像您的monster类的副作用一样
我看到了许多需要应用于代码的优化。
但是,这太长了,无法解释,正如示例所示:
1. sendGroupMessage You can check by MultiUserChatManager if you
already joined the chat and then send the message. You must fire a
"join" just once, not everytime you want to send a message.
二,<代码>mMultiUserChat.addMessageListener(此)代码>
必须添加一次侦听器,否则将创建大量线程。可能是因为你有一个单身汉。当你有一个监听器时,如果你不删除它,你就不需要再将它添加到聊天中
mConnection.addSyncStanzaListener(这个,null)代码>
小心:您正在将您的侦听器(哪一个?您使用同一个类实现了大量侦听器)添加到您的连接中。在这之前或之后,您的代码将吃掉一个重要的部分(通常是一个自定义IQ),并且您将有一个难以发现的副作用李>
mConnection.addAsyncStanzaListener(这个,null)代码>与3相同
检查是否有ProviderManager.addExtensionProvider()
,在之前或之后
你需要一些
希望有帮助。试试这个
mConnection.addAsyncStanzaListener(this, null);
mConnection.addSyncStanzaListener(this, null);
private StanzaTypeFilter serverFilter;
private StanzaListener stanzaListener = null;
private XMPPTCPConnection mConnection;
registerStanzaListener(); // where you init connection
public void registerStanzaListener() {
serverFilter = new StanzaTypeFilter(Message.class);
if (stanzaListener != null) {
mConnection.removeAsyncStanzaListener(stanzaListener);
}
stanzaListener = new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
processMessage((Message) packet);
}
};
mConnection.addAsyncStanzaListener(stanzaListener, serverFilter);
}
}
步骤1:1删除此项
mConnection.addAsyncStanzaListener(this, null);
mConnection.addSyncStanzaListener(this, null);
private StanzaTypeFilter serverFilter;
private StanzaListener stanzaListener = null;
private XMPPTCPConnection mConnection;
registerStanzaListener(); // where you init connection
public void registerStanzaListener() {
serverFilter = new StanzaTypeFilter(Message.class);
if (stanzaListener != null) {
mConnection.removeAsyncStanzaListener(stanzaListener);
}
stanzaListener = new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
processMessage((Message) packet);
}
};
mConnection.addAsyncStanzaListener(stanzaListener, serverFilter);
}
}
步骤2:添加此项
mConnection.addAsyncStanzaListener(this, null);
mConnection.addSyncStanzaListener(this, null);
private StanzaTypeFilter serverFilter;
private StanzaListener stanzaListener = null;
private XMPPTCPConnection mConnection;
registerStanzaListener(); // where you init connection
public void registerStanzaListener() {
serverFilter = new StanzaTypeFilter(Message.class);
if (stanzaListener != null) {
mConnection.removeAsyncStanzaListener(stanzaListener);
}
stanzaListener = new StanzaListener() {
@Override
public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
processMessage((Message) packet);
}
};
mConnection.addAsyncStanzaListener(stanzaListener, serverFilter);
}
}
谢谢你的回答…但我不明白如果我的代码是错误的,那么当两个用户在同一个群中开始互相发送消息时,为什么群聊可以正常工作。InvitationListener仅在我需要邀请群成员时使用,但是我的聊天和what's应用程序一样..所以我必须自动加入..如果我不适合群聊,那么你能给我正确的方向吗;表示您将“this class”(哪个类?)附加为messageListener。代码片段太短,无法理解,但我真的不明白重点,如果它不是一个邀请监听器,并且它是正确的,那么你的代码它正在创建一个MessageListener,在它的构造中创建一个groupchat?然而,添加一个SyncStanzaListener与它不需要的类相同,我建议尝试删除这行代码。您好,谢谢您的回答。请检查我的课堂,让我知道哪里错了,请纠正我。。检查网址:你好,谢谢你的黄金时间。让我检查一下我自己。你需要实现“总是在群里聊天”,也就是说,如果有两个用户彼此聊天,而不是您需要将他们放入组中,则不直接在用户id中发送消息。如果组中有多个用户,而不是在公共组id中发送消息,并且哪些用户具有相同的组id,则这些用户将接收消息。@VishalPatoliyaツ 我们使用的是相同的,我们已经使用了组id。对于单个聊天,您实现了按组id聊天?当您向特定用户发送消息时,您发送的是用户id或组id消息?对于使用用户id的一对一聊天,以及对于始终使用组id的组聊天