When I open a chat window of the particular group then, I create/join a group by below code
private MultiUserChat joinRoom(String roomName) throws XmppStringprepException, XMPPException.XMPPErrorException, MultiUserChatException.NotAMucServiceException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException, MultiUserChatException.MucAlreadyJoinedException {
if (roomName.equals("")) {
logAndToast("Enter room name");
return null;
}
if (MyXMPP.connection != null && MyXMPP.connection.isAuthenticated()) {
// Get the MultiUserChatManager
MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(MyXMPP.connection);
// Create the XMPP address (JID) of the MUC.
EntityBareJid mucJid = (EntityBareJid) JidCreate.bareFrom(XMPPHelper.getRoomName(roomName));
// Create a MultiUserChat using an XMPPConnection for a room
MultiUserChat muc2 = manager.getMultiUserChat(mucJid);
// User2 joins the new room
// The room service will decide the amount of history to send
// Create the nickname.
Resourcepart nickname = Resourcepart.from(PreferenceManager.getStringPreference(this, PreferenceManager.XMPP_USER_NAME));
MucEnterConfiguration.Builder mec = muc2.getEnterConfigurationBuilder(nickname);
MucEnterConfiguration mucEnterConfig = mec.build();
muc2.join(mucEnterConfig);
return muc2;
}
return null;
}//end of MultiUserChat()
After that, I set incoming message listener for the group as below
multiUserChat = joinRoom("myRoomName"));
if (multiUserChat != null) {
multiUserChat.addMessageListener(new MessageListener() {
@Override
public void processMessage(final Message message) {
//here I received messages for the perticular group that I joined
}
});
if (multiUserChat.isJoined()) {
logAndToast("join xmpp room successfully");
} else {
logAndToast("join xmpp room not joined");
}
}
I received all the messages by the above code when I am in a particular group window.
I need a solution for how to receive all the incoming messages in all the groups when I outside the group window.
I referred below links for MUC group chat https://download.igniterealtime.org/smack/docs/latest/documentation/extensions/muc.html
I searched/visited many links but could not found any solution that is useful for me. Please help me to provide a solution.
Thanks in Advance.
To get any group message without join or out side the chat window you need to subscribe that group.
We can achieve the same functionality using MucSub: Multi-User Chat Subscriptions https://docs.ejabberd.im/developer/xmpp-clients-bots/extensions/muc-sub/
We can subscribe group using below code for custom message
You can listen your success failure of custom message by this code
Once you have successfully subscribed that group you can listen incoming message for that subscribed group using below code