菜鸟搞环信
Posted 世界仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了菜鸟搞环信相关的知识,希望对你有一定的参考价值。
刚完成了环信的相关工作,刚好有时间来写一下关于环信的东西
马上进入主题吧
1.关于如何集成环信。
集成环信的话就不多说了
2.如何修改环信的昵称和头像。
这个我认为是重点。我也是一个新手,都是一步一个脚印,慢慢踩踏过来的。
我的做法是分两个,一个是利用环信的ext扩展作为突破口。第二个是利用本地缓存作为突破口。
首先说一下,为什么有两个?
因为,我们在发消息的时候,可以利用环信的ext扩展,把头像之类的东西,存到消息里面。这样,我们在发消息的时候就可以获得头像之类的东西,然后就可以更新chatview。这个仅仅作用于发消息。在我们接收到一条新消息的时候,就不能这么搞了。因为,如果你之前没有发送过消息给他,那么你的ext扩展就不能穿过去给他。这样就会造成只有你发过消息的人才能正常显示。这里就需要用到本地缓存了。
大致的思路就这样,那么我们就分散一步一步来~~
A。首先把单聊发消息的这个给先实现。
从这里push到环信的会话列表。push之前安全起见,先本地化头像。。
- (void)pushToChatViewController{
/// 新建一个会话
if ([self.model.user_no isEqualToString:[UserInfo shareUserInfo].user_no]) {
[MBProgressHUD showAndDismissInWindow:@"不能与自己聊天"];
return;
}
NSString *name = self.model.user_niname;
if (name.length == 0 || [name isEqualToString:@""]) {
name = @"xx";
}
//TODO:这里我只是为了测试
NSString *img = self.model.user_headimg;
if (img.length == 0 || [img isEqualToString:@""]) {
img = @"";
}
//保存用户的昵称和头像到数据库
MCNickNameDB *nickNameDB = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",name]];
if (nickNameDB) {
MCNickNameDB *n = [MCNickNameDB new];
n.conversationId = self.userId;
n.niname = name;
n.headimg = img;
BOOL nick = [nickNameDB.niname isEqualToString:n.niname];
BOOL img = [nickNameDB.headimg isEqualToString:n.headimg];
if (!nick || !img) {
[MCNickNameDB deleteObjectsByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",n.conversationId]];
[n save];
MCNickNameDB *nickNameDB2 = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",n.conversationId]];
DLog(@"%@",nickNameDB2);
}
}else{
MCNickNameDB *n = [MCNickNameDB new];
n.conversationId = self.userId;
n.niname = name;
n.headimg = img;
[n save];
}
NSDictionary *ext = @{
EASE_Nicname_Other_Key:name,
EASE_Avatar_Other_Key:img
};
ChatViewController *chatController = [[ChatViewController alloc] initWithConversationChatter:_userId conversationType:EMConversationTypeChat ext:ext];
DLog(@"新建一个会话 ext == %@ ",ext);
chatController.title = _model.user_niname;
[self.navigationController pushViewController:chatController animated:YES];
}
既然是发消息,那么久肯定跟EaseChatToolbar输入框有关了
找到EaseChatToolbar
- (void)sendFaceWithEmotion:(EaseEmotion *)emotion{
if (emotion) {
if ([self.delegate respondsToSelector:@selector(didSendText:withExt:)]) {
NSString *name = EASE_Nik_Name_Value;
NSString *img = EASE_Avatar_Value;
if ([name isEqualToString:@""] || name.length == 0) {
name = EASE_Nik_Name_Def;
}
if ([img isEqualToString:@""] || img.length == 0) {
img = EASE_Avatar_Def;
}
/// 兔斯基表情文本扩展
NSDictionary *ext = @{
EASEUI_EMOTION_DEFAULT_EXT:emotion,
EASE_Avatar_Key:img,
EASE_Nik_Name_Key:name
};
[self.delegate didSendText:emotion.emotionTitle withExt:ext];
[self _willShowInputTextViewToHeight:[self _getTextViewContentH:self.inputTextView]];;
}
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
if ([self.delegate respondsToSelector:@selector(didSendText:)]) {
NSString *name = EASE_Nik_Name_Value;
NSString *img = EASE_Avatar_Value;
if ([name isEqualToString:@""] || name.length == 0) {
name = EASE_Nik_Name_Def;
}
if ([img isEqualToString:@""] || img.length == 0) {
img = EASE_Avatar_Def;
}
NSDictionary *ext = @{
EASE_Avatar_Key:img,
EASE_Nik_Name_Key:name
};
[self.delegate didSendText:textView.text withExt:ext];
//结束
self.inputTextView.text = @"";
[self _willShowInputTextViewToHeight:[self _getTextViewContentH:self.inputTextView]];
}
return NO;
}
else if ([text isEqualToString:@"@"]) {
if ([self.delegate respondsToSelector:@selector(didInputAtInLocation:)]) {
if ([self.delegate didInputAtInLocation:range.location]) {
[self _willShowInputTextViewToHeight:[self _getTextViewContentH:self.inputTextView]];
return NO;
}
}
}
else if ([text length] == 0) {
//delete one character
if (range.length == 1 && [self.delegate respondsToSelector:@selector(didDeleteCharacterFromLocation:)]) {
return ![self.delegate didDeleteCharacterFromLocation:range.location];
}
}
return YES;
}
在这两个方法上面 ,添加ext。。其实细心的人可以发现EaseChatToolbar的delegate是EaseMessageViewController
所以也可以
我比较建议在EaseMessageViewController,因为不管什么类型的消息都是走这个类的
代码如下
- (void)didSendText:(NSString *)text withExt:(NSDictionary*)ext
{
//TODO:这里是不包括 add的
DLog(@"消息的携带体是ext == %@",ext);
DLog(@"img == %@, name == %@",ext[EASE_Avatar_Key],ext[EASE_Nik_Name_Def]);
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
dic[EASE_Avatar_Key] = ext[EASE_Avatar_Key];
dic[EASE_Nik_Name_Def] = ext[EASE_Nik_Name_Def];
DLog(@"conversation == %@",_conversation.conversationId);
if (self.conversation.type == 1) {/// 群聊
MCNickNameDB *nickNameDB = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",_conversation.conversationId]];
dic[EASE_GroupID__Key] = nickNameDB.conversationId;
dic[EASE_Nicname_Group_Key] = nickNameDB.niname;
dic[EASE_Avatar_Group_Key] = nickNameDB.headimg;
DLog(@"EASE_GroupID__Key == %@",dic[EASE_Nicname_Group_Key]);
}
if ([ext objectForKey:EASEUI_EMOTION_DEFAULT_EXT]) {
EaseEmotion *emotion = [ext objectForKey:EASEUI_EMOTION_DEFAULT_EXT];
if (self.dataSource && [self.dataSource respondsToSelector:@selector(emotionExtFormessageViewController:easeEmotion:)]) {
/// 这里是纯兔斯基 发送
NSDictionary *emojiDic = [self.dataSource emotionExtFormessageViewController:self easeEmotion:emotion];
if (self.conversation.type == 1) {/// 群聊
dic[@"em_expression_id"] = emojiDic[@"em_expression_id"];
dic[@"em_is_big_expression"] = emojiDic[@"em_is_big_expression"];
}
[self sendTextMessage:emotion.emotionTitle withExt:dic];
} else {
// 这里需要加上消息扩张
if (self.conversation.type == 0) {
[self sendTextMessage:emotion.emotionTitle withExt:
@{
MESSAGE_ATTR_EXPRESSION_ID:emotion.emotionId,MESSAGE_ATTR_IS_BIG_EXPRESSION:@(YES),
EASE_Avatar_Key:dic[EASE_Avatar_Key],
EASE_Nik_Name_Key:dic[EASE_Nik_Name_Key]
}
];
}else{/// 群聊
[self sendTextMessage:emotion.emotionTitle withExt:
@{
MESSAGE_ATTR_EXPRESSION_ID:emotion.emotionId,MESSAGE_ATTR_IS_BIG_EXPRESSION:@(YES),
EASE_Avatar_Key:dic[EASE_Avatar_Key],
EASE_Nik_Name_Key:dic[EASE_Nik_Name_Key],
EASE_Avatar_Group_Key:dic[EASE_Avatar_Group_Key],
EASE_Nicname_Group_Key:dic[EASE_Nicname_Group_Key],
EASE_GroupID__Key:dic[EASE_GroupID__Key]
}
];
}
}
return;
}
if (text && text.length > 0) {
/// 纯文字副本发送
if (self.conversation.type == 0) {
[self sendTextMessage:text withExt:ext];
}else{/// 群聊
[self sendTextMessage:text withExt:dic];
}
}
}
以上标有群聊的可以先忽略不做
在所有发消息的(兔斯基,表情,文字,表情+文字等等)统统加上ext
例如
- (void)sendTextMessage:(NSString *)text
{
// NSDictionary *ext = nil;
/// 在这里添加emoji 头像
NSString *name = EASE_Nik_Name_Value;
NSString *img = EASE_Avatar_Value;
if ([name isEqualToString:@""] || name.length == 0) {
name = EASE_Nik_Name_Def;
}
if ([img isEqualToString:@""] || img.length == 0) {
img = EASE_Avatar_Def;
}
NSMutableDictionary *ext = [NSMutableDictionary dictionary];
ext[EASE_Avatar_Key] = img;
ext[EASE_Nik_Name_Key] = name;
if (self.conversation.type == 1) {//群聊
MCNickNameDB *nickNameDB = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",_conversation.conversationId]];
ext[EASE_GroupID__Key] = nickNameDB.conversationId;
ext[EASE_Nicname_Group_Key] = nickNameDB.niname;
ext[EASE_Avatar_Group_Key] = nickNameDB.headimg;
DLog(@"EASE_GroupID__Key == %@",ext[EASE_Nicname_Group_Key]);
}
if (self.conversation.type == EMConversationTypeGroupChat) {
NSArray *targets = [self _searchAtTargets:text];
if ([targets count]) {
__block BOOL atAll = NO;
[targets enumerateObjectsUsingBlock:^(NSString *target, NSUInteger idx, BOOL *stop) {
if ([target compare:kGroupMessageAtAll options:NSCaseInsensitiveSearch] == NSOrderedSame) {
atAll = YES;
*stop = YES;
}
}];
if (atAll) {
ext = @{kGroupMessageAtList: kGroupMessageAtAll};
}
else {
ext = @{kGroupMessageAtList: targets};
}
}
}
[self sendTextMessage:text withExt:ext];
}
那么,所有的消息都带上了ext,基本上发消息的就完成了。
回到chatViewController里面
- (NSDictionary*)emotionExtFormessageViewController:(EaseMessageViewController *)viewController
easeEmotion:(EaseEmotion*)easeEmotion{
/// 兔斯基表情文本扩展
NSString *name = EASE_Nik_Name_Value;
NSString *img = EASE_Avatar_Value;
if ([name isEqualToString:@""] || name.length == 0) {
name = EASE_Nik_Name_Def;
}
if ([img isEqualToString:@""] || img.length == 0) {
img = EASE_Avatar_Def;
}
return @{
MESSAGE_ATTR_EXPRESSION_ID:easeEmotion.emotionId,MESSAGE_ATTR_IS_BIG_EXPRESSION:@(YES),
EASE_Avatar_Key:img,
EASE_Nik_Name_Key:name
};
}
- (id<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController
modelForMessage:(EMMessage *)message{
id<IMessageModel> model = nil;
model = [[EaseMessageModel alloc] initWithMessage:message];
//TODO:这里设置环信的头像
NSDictionary *dic = message.ext;
NSString *userName = dic[EASE_Nik_Name_Key];
NSString *sendUserImg = dic[EASE_Avatar_Key];
model.avatarURLPath = sendUserImg;
model.nickname = userName;
UserProfileEntity *profileEntity = [[UserProfileManager sharedInstance] getUserProfileByUsername:model.nickname];
if (profileEntity) {
model.avatarURLPath = profileEntity.imageUrl;
model.nickname = profileEntity.nickname;
}
model.failImageName = @"imageDownloadFail";
return model;
}
这样发消息就ok了。。。
下面就说一下接收消息,接收消息我用本地缓存。
接收消息的话,就找chatdemohelper
找到
/// 这里好重要 就是收到消息扩张
- (void)didReceiveMessages:(NSArray *)aMessages
{
//保存用户的昵称和头像到数据库
BOOL isRefreshCons = YES;
for(EMMessage *message in aMessages){
DLog(@"EMChatType == %d ext == %@",message.chatType,message.ext);
if (message.chatType == 0) { // 单聊
//保存用户的昵称和头像到数据库
MCNickNameDB *nickNameDB = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",message.conversationId]];
if (nickNameDB) {
MCNickNameDB *n = [MCNickNameDB new];
n.conversationId = message.conversationId;
n.niname = message.ext[EASE_Nik_Name_Key];
n.headimg = message.ext[EASE_Avatar_Key];
BOOL nick = [nickNameDB.niname isEqualToString:n.niname];
BOOL img = [nickNameDB.headimg isEqualToString:n.headimg];
if (!nick || !img) {
[MCNickNameDB deleteObjectsByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",message.conversationId]];
[n save];
MCNickNameDB *nickNameDB2 = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",message.conversationId]];
DLog(@"%@",nickNameDB2);
}
}else{
MCNickNameDB *n = [MCNickNameDB new];
n.conversationId = message.conversationId;
n.niname = message.ext[EASE_Nik_Name_Key];
n.headimg = message.ext[EASE_Avatar_Key];
[n save];
}
}else{ //TODO: 聊天时 群聊
//保存群聊的昵称和头像到数据库
NSDictionary *ext = message.ext;
MCNickNameDB *nickNameDB = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",message.conversationId]];
if (nickNameDB) {
MCNickNameDB *n = [MCNickNameDB new];
n.conversationId = ext[EASE_GroupID__Key];
n.niname = ext[EASE_Nicname_Group_Key];
n.headimg = ext[EASE_Avatar_Group_Key];
BOOL nick = [nickNameDB.niname isEqualToString:n.niname];
BOOL img = [nickNameDB.headimg isEqualToString:n.headimg];
if (!nick || !img) {
[MCNickNameDB deleteObjectsByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",n.conversationId]];
[n save];
MCNickNameDB *nickNameDB2 = [MCNickNameDB findFirstByCriteria:[NSString stringWithFormat:@" WHERE conversationId = %@ ",n.conversationId]];
DLog(@"%@",nickNameDB2);
}
}else{
MCNickNameDB *n = [MCNickNameDB new];
n.conversationId = ext[EASE_GroupID__Key];
n.niname = ext[EASE_Nicname_Group_Key];;
n.headimg = ext[EASE_Avatar_Group_Key];
[n save];
}
}
BOOL needShowNotification = (message.chatType != EMChatTypeChat) ? [self _needShowNotification:message.conversationId] : YES;
#ifdef REDPACKET_AVALABLE
/**
* 屏蔽红包被抢消息的提示
*/
NSDictionary *dict = message.ext;
needShowNotification = (dict && [dict valueForKey:RedpacketKeyRedpacketTakenMessageSign]) ? NO : needShowNotification;
#endif
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (needShowNotification) {
#if !TARGET_IPHONE_SIMULATOR
switch (state) {
case UIApplicationStateActive:
[self.mainVC playSoundAndVibration];
break;
case UIApplicationStateInactive:
[self.mainVC playSoundAndVibration];
break;
case UIApplicationStateBackground:
[self.mainVC showNotificationWithMessage:message];
break;
default:
break;
}
#endif
}
if (_chatVC == nil) {
_chatVC = [self _getCurrentChatView];
}
BOOL isChatting = NO;
if (_chatVC) {
isChatting = [message.conversationId isEqualToString:_chatVC.conversation.conversationId];
}
if (_chatVC == nil || !isChatting || state == UIApplicationStateBackground) {
[self _handleReceivedAtMessage:message];
if (self.conversationListVC) {
[_conversationListVC refresh];
}
if (self.mainVC) {
[_mainVC setupUnreadMessageCount];
}
return;
}
if (isChatting) {
isRefreshCons = NO;
}
}
if (isRefreshCons) {
if (self.conversationListVC) {
[_conversationListVC refresh];
}
if (self.mainVC) {
[_mainVC setupUnreadMessageCount];
}
}
}
以上,,单聊的基本完成。。。。如果有遗漏,请留言,,
下面是群聊
其实群聊跟单聊是一样,,不过,就是给ext扩展再添加一个群的名称,一个群的头像。
然后在拿属性的时候,划分一下是群聊还是单聊。。。
这里就不一一到来,上面有标注群聊的大家可以看一下~~~
以上是关于菜鸟搞环信的主要内容,如果未能解决你的问题,请参考以下文章