Reputation: 412
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.
Upvotes: 0
Views: 395
Reputation: 412
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
fun subscribeMUCRoom(context: Context?, toGroupName: String) {
if (!isStrNotNull(toGroupName) || MyXMPP.Companion.connection == null) {
return
}
try {
val iq: IQ = object : IQ("subscribe", "urn:xmpp:mucsub:0") {
override fun getIQChildElementBuilder(xml: IQChildElementXmlStringBuilder): IQChildElementXmlStringBuilder {
xml.append(" nick=")
xml.append("'" + Your Display Name + "'")
xml.rightAngleBracket()
xml.append("""<event node='urn:xmpp:mucsub:nodes:messages' />
<event node='urn:xmpp:mucsub:nodes:affiliations' />
<event node='urn:xmpp:mucsub:nodes:subject' />
<event node='urn:xmpp:mucsub:nodes:config' />""")
return xml
}
}
iq.type = IQ.Type.set
iq.setTo(toGroupName+ "@conference.yourdomain.com)
iq.setFrom(userJid)
setIQ(iq)
connection?.sendStanza(iq)
Log.d(TAG, "subscribeMUCRoom :: " + iq.toString() + " :: " + iq.childElementXML)
} catch (e: Exception) {
e.printStackTrace()
}
}
You can listen your success failure of custom message by this code
@Throws(NotConnectedException::class, InterruptedException::class)
fun setIQ(iq: IQ?) {
MyXMPP.connection!!.sendIqWithResponseCallback(iq, PacketListener { packet -> d(TAG, "setIQ :: success : $packet" /*.toXML()*/) }, ExceptionCallback { exception ->
exception.printStackTrace()
d(TAG, "setIQ :: processException : " + exception.message)
}, 10000)
}
Once you have successfully subscribed that group you can listen incoming message for that subscribed group using below code
private fun groupMessageListener() {
connection!!.addAsyncStanzaListener(StanzaListener { packet ->
val message = packet as Message
}, AndFilter(StanzaTypeFilter.MESSAGE, MessageTypeFilter.GROUPCHAT))
}
Upvotes: 0