Reputation: 3399
I am developing a chat using quickblox but I am having some problems when I open a new chat. Suddenly I received all the messages that others users sends to me when I was disconnected. The problem is that when I start a chat with user A, I receive the chats from users B, C, D.. in user A chat room.
I have find the way to only show the A users. But the problem is that the server has already sent to me the "disconnected" messages, so when I start a chat to B I do not receive any text because the message that the user B sent to me has been delivered (and omitted) while I was chatting with user A.
How can I do to receive the pending messages (kind of history) or to just retrieve the message of the chat I am logged in?
A piece of my code:
private MessageListener messageListener = new MessageListener() {
@Override
public void processMessage(Chat chat, Message message) {
System.out.println("CridaC");
String from = message.getFrom().split("@")[0];
String to = message.getTo().split("@")[0];
System.out.println(String.format(">>> Message received (from=%s, to=%s): %s",from, to, message.getBody()));
// return;
if (onMessageReceivedListener != null && message.getBody() != null) {
System.out.println("CridaD");
onMessageReceivedListener.onMessageReceived(message);
}
}
};
Does anybody know I way to deploy a chat in a few steps?
Upvotes: 0
Views: 1158
Reputation: 4956
You need to get chat dialog for this particular user for which you wants to get history. Actually when two users starting chat at first time, a dialog is created for them to perform chat. They will always chat under this dialog. Dialog you can say is a kind of platform to chat. Dialog also has a dialog id. Quickblox provided a function to create dialog on behalf of friendID whom you wanna chat(You need to pass friendID into this function). If this is the first time chat, this function creates a dialogId for you and if you have performed a chat before then existing dialog ID is returned. After getting this dialogID, you are able to get chat history with the help of this dialogID.
Conclusion : You can only get chat history by dialogID.
Here is the code :
1.) Create a chatManager object.
2.) Create your custom message object by using QuickBlox QBChatMessage object :
public class ChatMessageModel {
private Integer id;
private String body;
private Collection<Integer> readIds;
private Integer recipientId;
private String nameToDisplayOnlyWhenAMessageIsReceived;
private String messageId;
private Integer senderId;
private String message;
private String dialogId;
private long time;
private boolean isMessageSelected=false; //true if user selects an item or copy or delete and vice versa
public boolean isMessageSelected() {
return isMessageSelected;
}
public void setIsMessageSelected(boolean isMessageSelected) {
this.isMessageSelected = isMessageSelected;
}
public String getMessageId() {
return messageId;
}
public void setMessageId(String messageId) {
this.messageId = messageId;
}
public Integer getSenderId() {
return senderId;
}
public void setSenderId(Integer senderId) {
this.senderId = senderId;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public String getBody() {
return body;
}
public String getNameToDisplayOnlyWhenAMessageIsReceived() {
return nameToDisplayOnlyWhenAMessageIsReceived;
}
public void setNameToDisplayOnlyWhenAMessageIsReceived(String nameToDisplayOnlyWhenAMessageIsReceived) {
this.nameToDisplayOnlyWhenAMessageIsReceived = nameToDisplayOnlyWhenAMessageIsReceived;
}
public void setBody(String body) {
this.body = body;
}
// public Collection<Integer> getReadIds() {
// return readIds;
// }
public void setReadIds(Collection<Integer> readIds) {
this.readIds = readIds;
}
public Integer getRecipientId() {
return recipientId;
}
public void setRecipientId(Integer recipientId) {
this.recipientId = recipientId;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public String getDialogId() {
return dialogId;
}
public void setDialogId(String dialogId) {
this.dialogId = dialogId;
}
public void setTime(long time) {
this.time = time;
}
public Long getTime() {
return time;
}
@Override
public boolean equals(Object obj) {
// return super.equals(o);
if (obj == null || obj.getClass() != ChatMessageModel.class) {
return false;
}
ChatMessageModel other = (ChatMessageModel) obj;
if (other.messageId != this.messageId) {
return false;
}
return true;
}
@Override
public int hashCode() {
// return super.hashCode();
return messageId == null ? 0 : messageId.hashCode();
}
@Override
public String toString() {
return messageId;
}
}
3.) Function to get chat history :
public void getChatHistoryForDialog(Integer otherUserId, final Integer numberOfMessages) {
final ArrayList<ChatMessageModel> myList = new ArrayList<ChatMessageModel>();
privateChatManager.createDialog(otherUserId, new QBEntityCallbackImpl<QBDialog>() {
@Override
public void onSuccess(final QBDialog dialog, Bundle args) {
QBRequestGetBuilder requestBuilder = new QBRequestGetBuilder();
requestBuilder.sortAsc("last_message_date_sent");
requestBuilder.setPagesLimit(numberOfMessages.intValue());// limit of max messages fetch from server
QBChatService.getDialogMessages(dialog, requestBuilder, new QBEntityCallbackImpl<ArrayList<QBChatMessage>>() {
@Override
public void onSuccess(ArrayList<QBChatMessage> messages, Bundle args) {
for (QBChatMessage message : messages) {
ChatMessageModel model = new ChatMessageModel();
model.setIsMessageSelected(false);
model.setId(message.getSenderId());
model.setMessageId(message.getId());
model.setMessage(message.getBody());
message.setMarkable(true);
model.setTime(message.getDateSent() * 1000L);// Multiplied by 1000L because the time given by Quickblox server is epoch time
model.setReadIds(message.getReadIds());
model.setRecipientId(message.getRecipientId());
model.setDialogId(dialog.getDialogId());
myList.add(model);
}
}
@Override
public void onError(List<String> errors) {
}
});
}
@Override
public void onError(List<String> errors) {
}
});
}
Upvotes: 1
Reputation: 18346
It is a normal behaviour. You will receive all the messages from all the users after just login. Doesn't matter if you are now in the Chat with user A.
Possible solution - store all new messages into the ArrayList, HashMap. When you open a chat with user B - just retrieve messages from user B from this ArrayList/ HashMap and show them in your screen.
Upvotes: 1