after sending a message it is receiving to the group. But at the same time again I'm receiving all the previous messages. Help me out of this. Here is my code:
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
to = groupname + "@conference.localhost".toString();
String text = (textMessage.getText().toString());
textMessage.setText("");
muc = new MultiUserChat(connection, to);
try {
muc.join(USERNAME);
} catch (XMPPException e) {
e.printStackTrace();
}
Message msg = new Message(to, Message.Type.groupchat);
msg.setBody(text);
if (connection != null) {
connection.sendPacket(msg);
Msg data = new Msg();
data.setMessage(text);
data.setName(userFrom);
data.setDate(DateAndTime.getCurrentDate());
data.setTime(DateAndTime.getCurrentTime());
data.setSender(true);
MessageListAdapter.messagesItems.add(data);
notifyMyAdapter();
}
}
});
and here is my receiving code:
public void setConnection(XMPPConnection connection) {
this.connection = connection;
if(connection != null) {
PacketFilter filter = new MessageTypeFilter(Message.Type.groupchat);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from " + fromName);
Msg data = new Msg();
String subject = message.getSubject();
if (subject == null) {
data.setMessage(message.getBody());
data.setName(NAME);
data.setDate(DateAndTime.getCurrentDate());
data.setTime(DateAndTime.getCurrentTime());
data.setSender(false);
MessageListAdapter.messagesItems.add(data);
mHandler.post(new Runnable() {
@Override
public void run() {
notifyMyAdapter();
}
});
} else if (subject.equals("")){
data.setMessage(message.getBody());
data.setName(NAME);
data.setDate(DateAndTime.getCurrentDate());
data.setTime(DateAndTime.getCurrentTime());
data.setSender(false);
MessageListAdapter.messagesItems.add(data);
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
notifyMyAdapter();
}
});
}
}
}
},filter);
}
}
In MultiUserChat
To control the amount of history to receive while joining a room you will need to provide a configured DiscussionHistory
object. for more info about MultiUserChat
refer this
muc.join(String nickname, String password, DiscussionHistory history, long timeout)
where DiscussionHistory
class controls the number of characters
or messages to receive when entering a room. The room will decide the
amount of history to return if you don't specify a
DiscussionHistory
while joining a room. for more info refer this
Example :
MultiUserChat muc = new MultiUserChat(connection, to);
DiscussionHistory history = new DiscussionHistory(); history.setMaxStanzas(0);
muc.join("username", "password", history, SmackConfiguration.getPacketReplyTimeout());