您需要获取要获取历史记录的特定用户的聊天对话框。实际上,当两个用户第一次开始聊天时,会创建一个对话框供他们进行聊天。他们将始终在此对话框下聊天。Dialog可以说是一种聊天平台。对话框也有一个对话框 id。Quickblox 提供了一个函数来代表您要聊天的朋友 ID 创建对话(您需要将朋友 ID 传递到此函数中)。如果这是第一次聊天,此函数会为您创建一个 dialogId,如果您之前进行过聊天,则返回现有的对话 ID。获得此 dialogID 后,您可以借助此 dialogID 获取聊天记录。
结论:只能通过 dialogID 获取聊天记录。
这是代码:
1.) 创建一个 chatManager 对象。
2.) 使用 QuickBlox QBChatMessage 对象创建您的自定义消息对象:
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.) 获取聊天记录功能:
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) {
}
});
}