Multiuserchat Send And Receive Message Error
I am develop android chat app (xmpp server -prosody- and android smack library) I created group room successfully and invite members but when I try to send message to the group th
Solution 1:
you have to first join the room before sending the XMPP message.
to join the xmpp room you have to send a presence stanza that will be like this:
<presencefrom='hag66@shakespeare.lit/pda'id='n13mt3l'to='coven@chat.shakespeare.lit/thirdwitch'><xxmlns='http://jabber.org/protocol/muc'/></presence>
In java this will be like:
PresencejoinPresence=newPresence(Presence.Type.available);
joinPresence.setTo(mThreadId);
joinPresence.addExtension(newMUCInitialPresence());
XMPPConnectionconx= Application.getInstance().getXMPPConection();
PacketFilterresponseFilter=newAndFilter(newFromMatchesFilter(mThreadId), newPacketTypeFilter(Presence.class));
PacketCollectorresponse= conx.createPacketCollector(responseFilter);
conx.sendPacket(joinPresence);
Presencepresence= (Presence) response.nextResult(SmackConfiguration.getPacketReplyTimeout());
response.cancel();
if (presence == null) {
Log.e("XMPP", "No response from server.");
} elseif (presence.getError() != null) {
Log.e("XMPP", presence.getError().toString());
}
Solution 2:
First of all, you need to join a room and make sure other group users also join the group as well, for that you have to send a group join invitation like below.
publicstaticvoidinviteToGroup(String inviteuser, String groupName) {
if (TextUtils.isEmpty(inviteuser) || TextUtils.isEmpty(groupName)) return;
try {
EntityBareJidmucJid= JidCreate.entityBareFrom(groupName + "@" + Constants.GRP_SERVICE);
Resourcepartnickname= Resourcepart.from(userId);
mucChatManager = MultiUserChatManager.getInstanceFor(MyApplication.connection);
mucChat = mucChatManager.getMultiUserChat(mucJid);
Messagemessage=newMessage();
// message.setType(Type.normal);
message.setSubject(Constants.GROUP_CHAT_MSG_MODE);
message.setBody(Constants.GROUP_GREETINGS);
EntityBareJideJId= JidCreate.entityBareFrom(inviteuser + "@" + Constants.XMPP_DOMAIN);
/*MucEnterConfiguration.Builder mucEnterConfiguration
= mucChat.getEnterConfigurationBuilder(nickname).requestHistorySince(sinceDate);*/
MucEnterConfiguration.BuildermucEnterConfiguration= mucChat.getEnterConfigurationBuilder(nickname).requestNoHistory();
mucChat.join(mucEnterConfiguration.build());
LogM.e("Room joined");
// mucChat.invite(message, eJId, groupName);
} catch (XmppStringprepException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (MultiUserChatException.NotAMucServiceException e) {
e.printStackTrace();
}
}
Here is my code for send Group Message you need to add message Type as a Type.groupchat
.
publicbooleansendGrpMessage(ChatPojo chatPojo, String grp_name) {
try {
finalStringbody= gson.toJson(chatPojo);
Messagemsg=newMessage();
msg.setType(Type.groupchat);
msg.setSubject("chat");
msg.setBody(body);
EntityBareJidmucJid= JidCreate.entityBareFrom(grp_name + "@" + Constants.GRP_SERVICE);
mucChatManager = MultiUserChatManager.getInstanceFor(MyApplication.connection);
mucChat = mucChatManager.getMultiUserChat(mucJid);
mucChat.sendMessage(msg);
//DataManager.getInstance().updateReceiptReceived(msgReceipt,Constants.MESSAGE_STATUS_NOT_DELIVERED);returntrue;
} catch (XmppStringprepException | InterruptedException | SmackException.NotConnectedException e) {
Log.d(TAG, "sendGrpMessage() Error = [" + e.getMessage() + "]");
returnfalse;
}
}
after that add group message listener
StanzaFilterfilter= MessageTypeFilter.GROUPCHAT;
MyApplication.connection.addAsyncStanzaListener(newStanzaListener() {
@OverridepublicvoidprocessStanza(Stanza packet)throws SmackException.NotConnectedException, InterruptedException {
Messagemessage= (Message) packet;
if (message.getType() == Type.groupchat && message.getBody() != null) {
LogM.e("+++++++++++++++++++++++++++GROUPCHAT+++++++++++++++++++++++++++++++++");
LogM.e("from: " + message.getFrom());
LogM.e("xml: " + message.getType().toString());
LogM.e("Got text [" + message.getBody() + "] from [" + message.getFrom() + "]");
LogM.e("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
} elseif (message.getType() == Type.error) {
Toast.makeText(service, "error type", Toast.LENGTH_SHORT).show();
}
}
}, filter);
Post a Comment for "Multiuserchat Send And Receive Message Error"