无法使用 smack-android:4.1.4 接收群聊消息 [英] Can't able to receive group chat messages using smack-android:4.1.4

查看:31
本文介绍了无法使用 smack-android:4.1.4 接收群聊消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 smack 库开发聊天应用程序.我在群聊中遇到问题.在我的应用程序中,我正在创建一个组,并且该组成员是自动加入的.即使他们没有发起聊天,我也想在组中发送消息时通知所有用户.我的代码如下所示将侦听器置于 init 方法中但无法接收消息.

I am developing chat app using smack library. I have an issue in group chat. In my app, i am creating a group and in that members are auto-joined.i want to notify all user when I send a message in the group even if they had not initiated a chat.My code is as follow in that I have place listener in init method but unable to receive a message.

        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无法接收消息,但是当用户 2 在该组内发送消息时,他们都可以接收消息.

I don't know why this method does not call when someone send message in group, even I joined group, If I create 1 group and joined 2 users, when 1 user sends message in group then user2 can't able to receive message, but when user 2 send message inside this group then they both are able to receive messages.

请帮助我,我无法找到解决方案.请不要给出已经过时的建议.

Please help me, I can't able to find the solution. Please don't give suggestion which is already deprecated.

提前致谢.!!

推荐答案

在完整的代码审查后,我正在完整地编辑答案.-再次-

我建议重构您的代码,以在 1 个以上的大类中保持角色分离.

I suggest to refactor your code to keep separation of roles in more than 1 huge class.

基本上,由于您的代码中有许多addasync - addsync",您在错误的侦听器中看到消息,并且您能够接收消息,就像您的怪物级全入的副作用一样!

Basically you are especting messages in wrong listener due to many "addasync - addsync" in your code and you are able to receive messages just as side effect of your monster-class-all-in!

我看到您需要对代码应用许多优化.解释太长,不可能,但是,举个例子:

I see many optimization you need to apply to your code. It's too long to explain and out of the question, however, just as example:

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.

2.mMultiUserChat.addMessageListener(this);必须添加一次侦听器,否则您将创建大量线程.可能它有效,因为你有一个单身人士.当您有一个听众时,如果您不将其删除,则无需再将其添加到该聊天中.

2. mMultiUserChat.addMessageListener(this); A listener must be added ONCE or you'll create tons of threads. Probably it works because you have a singleton. While you have a listener, you don't need to add it anymore to that chat if you don't remove it.

  1. mConnection.addSyncStanzaListener(this, null);小心:您正在将您的侦听器(只有一个?您实现了大量具有相同类的侦听器)添加到您的连接中.在您的代码之前或之后,您的代码会吃掉一个重要的节(prolly 自定义 IQ),并且您将很难发现副作用.
  2. mConnection.addAsyncStanzaListener(this, null); 与 3 相同
  3. 在之前或之后检查 ProviderManager.addExtensionProvider()你需要一些.
  1. mConnection.addSyncStanzaListener(this, null); Be carefull: you are adding your listener (wich one? You implements tons of listeners with same class) to your connection. Before or later your code will eat an important stanza (prolly a custom IQ) and you'll have an hard to discovery side effects.
  2. mConnection.addAsyncStanzaListener(this, null); same of 3
  3. Check for ProviderManager.addExtensionProvider(), before or later you'll need some.

希望有所帮助.

这篇关于无法使用 smack-android:4.1.4 接收群聊消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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