iOS开发(环信及时通讯)-获取聊天记录
之前公司集成环信聊天SDK,在聊天室开发开发过程中历史聊天记录获取卡了一点时间,现在将历史聊天记录的开发方法简单的记录一下,分享给大家
第一步:服务端获取环信聊天记录,提供接口返回数据
第二步:前端做消息类型判断,做处理
尽量将代码修改的很通俗易懂,大神不要喷,简单的东西容易理解,也是摸索中总结出来的
一:
服务端提供的查询接口,需要提供一些参数,以我的项目为例,有三个参,最重要的是一个分页字符,初次提供为一个空的字符串
所以在viewDidLoad 里的赋予一个记录分页的字符串为空值,
记录:cursorStr = @"";
请求:[self getInfoMoreMeassageRquest];
请求后,服务端返回的json数据大致如下:
{
action = get;
count = 50;
cursor = MTYxOTcyOTYyNTpnR2tBQVFNQWdHa0FCZ0ZUOU1SOWF3Q0FkUUFRVHg5dzZ2MWJFZVd2Rk9NRXJhdmxCQUNBZFFBUVR4OXc0UDFiRWVXMVBSR1FSeERuOXdB;
duration = 3990;
entities = (
{
"chat_type" = groupchat;
created = 1460513153714;
from = 100000013;
groupId = 135220650137616892;
modified = 1460513153714;
"msg_id" = 184461770679649224;
payload = {
bodies = (
{
msg = "\U5fae\U7b11\U30fb\U00b0\U9633\U5149\U52a0\U5165\U804a\U5929\U5ba4";
type = txt;
}
);
ext = {
"dasgang_type" = 0;
friendHeaderImage = "";
headurl = "http://chenggua.com/imagesUrl/20160407/201604070957593310.png";
"is_dashang" = 0;
"is_join_notice" = 1;
nickName = "\U5fae\U7b11\U30fb\U00b0\U9633\U5149";
roleName = "\U4f1a\U957f";
sex = 1;
userid = 100000013;
wealthGrade = 1;
};
};
timestamp = 1460513152817;
to = 135220650137616892;
type = chatmessage;
uuid = "40ac892a-011c-11e6-b8d6-89c365e78de2";
},
params = {
cursor = (
""
);
limit = (
50
);
ql = (
"select * where to='135220650137616892' order by timestamp desc "
);
};
path = "/chatmessages";
timestamp = 1460518366731;
uri = "http://a1.easemob.com/daowen/chenggua/chatmessages";
}
可以看到返回的数据里包含一个cursor = MTYxOTcyOTYyNTpnR2tBQVFNQWdHa0FCZ0ZUOU1SOWF3Q0FkUUFRVHg5dzZ2MWJFZVd2Rk9NRXJhdmxCQUNBZFFBUVR4OXc0UDFiRWVXMVBSR1FSeERuOXdB;
这个分页就是再次下拉的时候,加载第二页数据的时候需要提交到服务端请求
所以在请求中将此字符串赋值给 之前定义的记录字符串
cursorStr=jsonData[@"cursor"];
需要注意的是,如果聊天记录加载完毕之后,cursor会返回为空,所以在slimeRefresh delegate做了一下判断
if (_conversationType == eConversationTypeChatRoom){//这里我是判断聊天室才要历史记录,单聊不需要,可以忽略不管
if ([self isBlankString:cursorStr]) {//这个方法是判断是否为空
[_slimeView endRefresh];
return;
}else{
[self getInfoMoreMeassageRquest];
}
-----------------判断字符窜是否为空的方法
- (BOOL) isBlankString:(NSString *)string {
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
以上就是请求历史聊天记录部分
二:根据返回数据的消息类型做处理
简单的说就是根据不同的消息类型构建消息,然后插入到数据库就好了,直接上代码,我做了四中消息类型的判断,是可以直接复制拿去用的,:纯文字、图片、录音、地图位置
cursorStr=jsonData[@"cursor"];
NSMutableArray * arr=jsonData[@"entities"];
for (NSDictionary * dicc in arr) {
NSArray * xiaoxiArr = [NSArray arrayWithArray:dicc [@"payload"][@"bodies"]];
if ([[xiaoxiArr lastObject][@"type"]isEqualToString:@"txt"]) {//纯文字
EMChatText *chatText = [[EMChatText alloc] initWithText:[xiaoxiArr lastObject][@"msg"]];
EMTextMessageBody *textBody = [[EMTextMessageBody alloc] initWithChatObject:chatText];
EMMessage * message = [[EMMessage alloc]initMessageWithID:dicc[@"msg_id"] sender:dicc[@"from"] receiver:_conversation.chatter bodies:@[textBody]];
message.isRead = YES;
message.deliveryState = eMessageDeliveryState_Delivered;
message.ext = dicc [@"payload"][@"ext"];
message.messageType = eMessageTypeChatRoom;
message.timestamp = [dicc [@"timestamp"] longLongValue];
[[EaseMob sharedInstance].chatManager insertMessageToDB:message append2Chat:YES];
}else if ([[xiaoxiArr lastObject][@"type"]isEqualToString:@"img"]){//图片
NSDictionary * diccSize = [xiaoxiArr lastObject][@"size"];
NSDictionary *imageDic = @{EMMessageBodyAttrKeySecret:[xiaoxiArr lastObject][@"secret"],
EMMessageBodyAttrKeySize:@{EMMessageBodyAttrKeySizeWidth:[NSNumber numberWithFloat:[diccSize[@"width"] floatValue]],EMMessageBodyAttrKeySizeHeight:[NSNumber numberWithFloat:[diccSize[@"height"] floatValue]]},
EMMessageBodyAttrKeyFileName:@"image.jpg",
EMMessageBodyAttrKeyType:EMMessageBodyAttrTypeImag,
EMMessageBodyAttrKeyUrl:[xiaoxiArr lastObject][@"url"],
EMMessageBodyAttrKeyFileLength:[NSNumber numberWithInteger:[[xiaoxiArr lastObject][@"file_length"] integerValue]]};
EMImageMessageBody *imageBody = [EMImageMessageBody imageMessageBodyFromBodyDict:imageDic forChatter:_chatter];
EMMessage * image = [[EMMessage alloc]initMessageWithID:dicc[@"msg_id"] sender:dicc[@"from"] receiver:_conversation.chatter bodies:@[imageBody]];
image.timestamp = [dicc [@"timestamp"] longLongValue];
image.isReadAcked = YES;
image.messageType = eMessageTypeChatRoom;
image.deliveryState = eMessageDeliveryState_Delivered;
image.isRead = YES;
image.ext = dicc [@"payload"][@"ext"];
[[EaseMob sharedInstance].chatManager insertMessageToDB:image append2Chat:YES];
}else if ([[xiaoxiArr lastObject][@"type"]isEqualToString:@"audio"]){//音频
NSDictionary *voiceDic = @{EMMessageBodyAttrKeySecret:[xiaoxiArr lastObject][@"secret"],
EMMessageBodyAttrKeyFileName:[xiaoxiArr lastObject][@"filename"],
EMMessageBodyAttrKeyDuration:[NSNumber numberWithInteger:[[xiaoxiArr lastObject][@"length"] integerValue]],
EMMessageBodyAttrKeyType:EMMessageBodyAttrTypeAudio,
EMMessageBodyAttrKeyUrl:[xiaoxiArr lastObject][@"url"],
EMMessageBodyAttrKeyFileLength:@8870};
EMVoiceMessageBody *voiceBody = [EMVoiceMessageBody voiceMessageBodyFromBodyDict:voiceDic forChatter:_chatter];
EMMessage *voice = [[EMMessage alloc] initMessageWithID:dicc[@"msg_id"] sender:dicc[@"from"] receiver:_conversation.chatter bodies:@[voiceBody]];
voice.timestamp = [dicc [@"timestamp"] longLongValue];
voice.isReadAcked = YES;
voice.deliveryState = eMessageDeliveryState_Delivered;
voice.isRead = YES;
voice.ext = dicc [@"payload"][@"ext"];
voice.messageType = eMessageTypeChatRoom;
[[EaseMob sharedInstance].chatManager insertMessageToDB:voice append2Chat:YES];
}
else if ([[xiaoxiArr lastObject][@"type"]isEqualToString:@"loc"]){//地理位置
EMChatLocation * location = [[EMChatLocation alloc]initWithLatitude:[[xiaoxiArr lastObject][@"lat"] doubleValue] longitude:[[xiaoxiArr lastObject][@"lng"] doubleValue] address:[xiaoxiArr lastObject][@"addr"]];
EMLocationMessageBody * locBodiy = [[EMLocationMessageBody alloc]initWithChatObject:location];
EMMessage * loocc = [[EMMessage alloc]initMessageWithID:dicc[@"msg_id"] sender:dicc[@"from"] receiver:_conversation.chatter bodies:@[locBodiy]];
loocc.timestamp = [dicc [@"timestamp"] longLongValue];
loocc.isReadAcked = YES;
loocc.ext = dicc [@"payload"][@"ext"];
loocc.isRead = YES;
loocc.messageType = eMessageTypeChatRoom;
[[EaseMob sharedInstance].chatManager insertMessageToDB:loocc append2Chat:YES];
}
}
简单的小需求,对于第一次做环信的小白来说应该有些帮助,堆字水平不咋地,还希望大家多多沟通,共同交流