2
votes

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.!!

2
You need to implement chatting in always in group , I.e If there are two user are chat with each other than you need to put them in group not directly send message in user id If there are multiple user in group than send message in common group id and which user have same group id those will receive messages.Vishal Patoliya ツ
@VishalPatoliyaツ we are using same, we already follow group id.Mahesh Kavathiya
for single chat you implemented chat by group id?Vishal Patoliya ツ
when you send message to particular user than you send message on user id or group id?Vishal Patoliya ツ
for one to one using user id and for group chat always using group IdMahesh Kavathiya

2 Answers

1
votes

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.

1
votes

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

    }