-->

How to get offline message of group chat from Open

2020-07-23 09:25发布

问题:

Is any way in xmpp that i get offline message of MultiUserChat, when my user login and join room.

I want implement group chat like WhatsApp, Is any other way to implement this please suggest

Thanks in advance

回答1:

At least in ejjaberd when you enter the chat group, you have to enter your last timestamp, given that timestamp you will receive the messages from that moment.

Save the timestamp from your last message, and when you enter to your room, like the following:

 MultiUserChat muc = new MultiUserChat(mConnection, room_name);
                Log.d(TAG, "JOINING => " + room_name);

                DiscussionHistory history = new DiscussionHistory();
                if (mLastMessageDate == null)
                    history.setMaxStanzas(300);
                else
                    history.setSince(mLastMessageDate); //timestamp from your last message

 muc.join(mNickName, null, history,
                            SmackConfiguration.getDefaultPacketReplyTimeout());

Hope it helps



回答2:

First declare a MultiUserChat this way

private static MultiUserChat muc = null;

then in your oncreate method instantiate it this way

muc = new MultiUserChat(CONNECTION, room);
        try {
            muc.join(USERJID);
        } catch (SmackException.NoResponseException e) {
            e.printStackTrace();
        } catch (XMPPException.XMPPErrorException e) {
            e.printStackTrace();
        } catch (SmackException.NotConnectedException e) {
            e.printStackTrace();
        }

and call this method in the beginning of your app

void setMessageListner() {
        muc.addMessageListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) throws SmackException.NotConnectedException {
                Message msg = (Message)packet;
                msg.setSubject(msg,getBody);
                Logger.i("Received message  : "+msg.getBody()+" From "+msg.getSubject());
        });
    }

this way whenever the user get into a GroupChat he will get the last Messages of the group