2

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();
        }

Here is message listener of group

 @Override
public void processMessage(Message message) { 
    Logg.e(TAG,"Message received group.."); 
}

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.

Thanks in Advance.!!

Mahesh Kavathiya
  • 573
  • 5
  • 23

2 Answers2

1

I'm full editing answer after full code review. -again-

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

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); 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); 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.

Hope that helps.

MrPk
  • 2,862
  • 2
  • 20
  • 26
  • Thanks for you answer...but I don't understand if my code is wrong then why group chat is working proper when both users start messaging each other in the same group. – Mahesh Kavathiya Nov 22 '16 at 06:06
  • InvitationListener only used when i need to invite group member, but my chat is same like what's app..so i have to auto joined..if i am wrong for group chat so can you please give me proper direction for the same. – Mahesh Kavathiya Nov 22 '16 at 06:24
  • well, mMultiUserChat.addMessageListener(this); means you are attaching "this class" (wich one?) as messageListener. Snippets it's too short to understaind but I really don't get the point, if it's not an InvitationListener and it's correct, your code it's creating a MessageListener and in its construcor creating a groupchat? However add a SyncStanzaListener with same class it's not needed, I suggest to try to remove that line of code. – MrPk Nov 22 '16 at 09:31
  • Hello thanks for answer. please check my class and let me know where i am wrong and please correct me.. Check URL : http://keepnote.cc/jebel-4534 – Mahesh Kavathiya Nov 22 '16 at 09:40
  • Hello, Thanks for your golden time..let me check my self. – Mahesh Kavathiya Nov 22 '16 at 10:08
  • void processMessage(Chat chat, Message message); this is for 1 vs 1 chat @MrPk – Mahesh Kavathiya Nov 22 '16 at 10:13
  • void processMessage(Chat chat, Message message); this is for 1 vs 1 chat & void processMessage(Message message) this is for group chat. are you sure I am in the wrong listener ? – Mahesh Kavathiya Nov 22 '16 at 10:19
  • Yes sorry. But it's the same: you are adding wrong listeners too many times. You'll recive the messages but with a chaos order and with strange behaviour. It's too long to explain SMACK architecture, but you image an HashMap to check, for each stanza, who can manage.They are NOT designed to be mutually exclusive. – MrPk Nov 22 '16 at 10:20
  • I just saw another strange thing: you don't have to compose a Message each time you send a message to groupchat since SMACK does itself. Just add Body and if you need the subject. That's why you send message by multiuserchat object. More, it's better to send the configuration of the chat before join. Look at those tutorials http://download.igniterealtime.org/smack/docs/latest/documentation/ – MrPk Nov 22 '16 at 10:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128701/discussion-between-mahesh-kavathiya-and-mrpk). – Mahesh Kavathiya Nov 22 '16 at 10:34
1

Try This

step1 : 1 remove this

mConnection.addAsyncStanzaListener(this, null);
mConnection.addSyncStanzaListener(this, null);

Step 2 : add this

 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);
    }

    }
Mahesh Kavathiya
  • 573
  • 5
  • 23
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45