سؤال

I am developing a chat application using Openfire and Asmack. I have a service running background to keep the connection alive and listen to incoming messages for all chats. So far, I've successfully implemented for one to one (single) chat. Code below is my listener for all single chat.

chatmanager = connection.getChatManager();
chatmanager.addChatListener(
    new ChatManagerListener() {
        @Override
        public void chatCreated(Chat chat, boolean createdLocally)
        {
            if (!createdLocally) {
                chat.addMessageListener(new MyNewMessageListener(getApplicationContext()));
            }
        }
});

But now my problem is how do i implement the same thing for MultiUserChat messages? Hope you guys could give me a helping hand over here. THANKS IN ADVANCE!

هل كانت مفيدة؟

المحلول

I solved my problem but not sure it is the best approach. However, hope this will help you guys and always welcome for better answer.

I first addInvitationListener and automatically join room once receive any invitation

MultiUserChat.addInvitationListener(connection, new InvitationListener() {

    @Override
    public void invitationReceived(Connection conn, String room, String inviter, String reason, String password, Message arg5) {
         MultiUserChat muc = new MultiUserChat(connection, room);
             try {
                  muc.join(connection.getUser().substring(0, connection.getUser().indexOf("@")));
             } catch (XMPPException e) {
            Log.d("Error","line 123 : "+e.toString());
            }
        muc.addMessageListener(new ServiceGroupMessageListener(getApplicationContext()));
    }
});

Next, i store all the entered room name into Database. Then create a function that would grab the room name from database and finally add message listener to each of the room. So that when next time i open the app again, it will have message listener bind to each chat room.

***make sure change room setting to persistent at openfire side

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top