最近项目里需要添加IM通讯功能,初期负责人说要使用极光IM,一段折磨期就此开始了。个人之前用过腾讯IM、融云、环信等,这是第一次使用极光IM。说实话极光这个IM确实需要改进的地方很多,不吐不快:
1、集成不方便,没有自带的UI组件,对于想快速实现IM聊天功能的需要谨慎选择。后来经过查找资料发现网上有一套通用的IM UI组件即 Aurora IMUI,据说也是极光公司的,兼容极光IM,即便如此集成后仍然需要编写大量的代码实现功能。
2、极光IM登录、注册、以及修改密码 需要 App用户的密码,而其他IM最多提供一个用户唯一标识即可。App用户密码修改后需要把极光IM对应的用户密码同步下,否则极光IM有可能登不上。
3、Aurora IMUI中视频信息不能点击播放、语音可以直接点击播放,另外发布的图片没有点击查看大图功能,还需要自己去实现非常费时费事。
4、另外Aurora IMUI是一套 Swift 语言开发的UI,虽然支持CocoPods 集成到项目里面,但对ObjC的项目使用则有些麻烦。
以上就是感觉需要改进或优化的的地方。下面贴上代码供学习及参考:
//
// IMConversationViewController.m
//
#import "IMConversationViewController.h"
#import <Photos/Photos.h>
#import "MessageModel.h"
#import "UserModel.h"
#import "MessageEventModel.h"
#import "MessageEventCollectionViewCell.h"
#import <JMessage/JMSGVoiceContent.h>
#define cell_identify [[MessageEventCollectionViewCell class] description]
//会话时间格式
#define conversation_datetime_format @"MM-dd HH:mm"
//每页显示的会话条数
#define conversation_limit 10
@interface IMConversationViewController ()<IMUIMessageMessageCollectionViewDelegate,IMUIInputViewDelegate,IMUICustomInputViewDelegate,JMessageDelegate>{
//是否有加载更多
BOOL isMoreData;
//开始会话的位置
NSInteger pOffset;
MJRefreshNormalHeader *header;
}
//聊天信息列表
@property (weak, nonatomic) IBOutlet IMUIMessageCollectionView *messageList;
//底部输入面板
@property (weak, nonatomic) IBOutlet IMUIInputView *imuiInputView;
//当前登录者
@property (nonatomic,strong) UserModel *currentUser;
//顶部约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@end
@implementation IMConversationViewController
- (void)viewDidLoad {
[super viewDidLoad];
isMoreData = YES; //默认有加载更多
pOffset = 0; //开始的数据条数
//MARK:设置顶部约束
self.topConstraint.constant = K_APP_NAVIGATION_BAR_HEIGHT;
//背景颜色
self.view.backgroundColor = K_APP_VIEWCONTROLLER_BACKGROUND_GRAY_COLOR;
// Do any additional setup after loading the view from its nib.
[self initView];
//MARK:下拉加载更多消息
__weak typeof(self) weakSelf = self;
header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[weakSelf requestLoadHistoryMessage:NO];
}];
//隐藏时间
header.lastUpdatedTimeLabel.hidden = YES;
//设置文字
[header setTitle:@"下载加载更多" forState:MJRefreshStateIdle];
[header setTitle:@"开始加载" forState:MJRefreshStatePulling];
[header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
self.messageList.messageCollectionView.mj_header = header;
//登录判断
if (![self userIsLogin]) {
[self togoLogin:^{
[self loadMessageWithConversation:self.singleConversionm
AndLoading:YES
AndRefresh:YES];
}];
return;
}
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//注册键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
//当前用户设置
self.currentUser = [UserModel new];
//创建单聊会话
[self initSingleMessage];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillChangeFrameNotification
object:nil];
//销毁会话
if(self.singleConversionm){
//移除消息监听
[JMessage removeDelegate:self withConversation:self.singleConversionm];
NSLog(@"会话已销毁");
}
}
//MARK: - initView
-(void)initView{
//MARK:导航
[self initNavgationBar:K_APP_NAVIGATION_BACKGROUND_COLOR
AndHasBottomLine:NO
AndHasShadow:NO
WithHasOffset:0.0];
//MARK:返回
[self initNavigationBack:K_APP_WHOTE_BACK];
//MARK:标题
[self initViewControllerTitle:@"客服"
ANDFontColor:K_APP_VIEWCONTROLLER_TITLE_COLOR
AndHasBold:NO
AndFontSize:K_APP_VIEWCONTROLLER_TITLE_FONT];
//MARK:信息展示列表
self.messageList.delegate = self;
[self.messageList.messageCollectionView registerClass:[MessageEventCollectionViewCell class]
forCellWithReuseIdentifier:cell_identify];
//MARK:底部输入面板
self.imuiInputView.delegate = self;
self.imuiInputView.inputViewDelegate = self;
}
//MARK: - 单聊初始化
/** 创建单聊会话 */
-(void)initSingleMessage{
//先获取
JMSGConversation *singleConversionm = [JMSGConversation singleConversationWithUsername:self.strServiceUserName];
//不存在则创建
if (singleConversionm == nil) {
__block typeof(self) blockSelf = self;
[JMSGConversation createSingleConversationWithUsername:self.strServiceUserName completionHandler:^(id resultObject, NSError *error) {
if (!error) {
//创建单聊会话成功, resultObject为创建的会话
blockSelf.singleConversionm = resultObject;
//消息监听
[JMessage addDelegate:self withConversation:blockSelf.singleConversionm];
[blockSelf loadMessageWithConversation:blockSelf.singleConversionm AndLoading:YES AndRefresh:YES];
}
else {
//创建单聊会话失败
NSLog(@"创建单聊会话失败!详见:%@",error);
NSLog(@"self.strServiceUserName:%@",blockSelf.strServiceUserName);
[MBProgressHUD showError:error.localizedDescription];
}
}];
}
else{
if (!self.singleConversionm){
self.singleConversionm = singleConversionm;
//消息监听
[JMessage addDelegate:self withConversation:self.singleConversionm];
}
[self loadMessageWithConversation:self.singleConversionm AndLoading:YES AndRefresh:YES];
}
}
/** 加载当前聊天室的会话信息 */
-(void)loadMessageWithConversation:(JMSGConversation *)conversation AndLoading:(BOOL)isAnimation AndRefresh:(BOOL)isRefresh{
if (conversation != nil) {
//刷新
if(isRefresh){
isMoreData = YES;
pOffset = 0;
//先移除
[self.messageList removeAllMessages];
}
//加载更多
else{
pOffset = [self.messageList getMessageCount];
}
if (isAnimation) {
[MBProgressHUD showMessage:@"消息加载中..." toView:self.view];
}
NSArray<JMSGMessage *> *arrMessage = [conversation messageArrayFromNewestWithOffset:[NSNumber numberWithInteger:pOffset] limit:[NSNumber numberWithInt:conversation_limit]];
NSLog(@"加载聊天室:%@",arrMessage);
if (arrMessage && [arrMessage count] > 0) {
[self formatMessage:arrMessage WithType:isRefresh];
isMoreData = YES;
}
else{
//没有加载更多
isMoreData = NO;
}
[self stopAnimation];
}
}
//MARK: - 消息转换
-(void)formatMessage:(NSArray<JMSGMessage *> *)arrMessages WithType:(BOOL)refresh{
if (arrMessages && [arrMessages count] > 0) {
//消息排序(降序,最新的消息在最底部)
NSArray<JMSGMessage *> *newArrMessage;
if(refresh){
newArrMessage = [arrMessages sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return ((JMSGMessage *)obj1).timestamp.longLongValue >= ((JMSGMessage *)obj2).timestamp.longLongValue;
}];
}
else{
newArrMessage = [arrMessages sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return ((JMSGMessage *)obj1).timestamp.longLongValue < ((JMSGMessage *)obj2).timestamp.longLongValue;
}];
}
MessageModel *msg;
for (JMSGMessage *jmessage in newArrMessage) {
msg = [IMConversationViewController getMessageModel:jmessage
WithServiceUser:self.strServiceUserName];
if (msg) {
if(refresh){
[self.messageList appendMessageWith:msg];
}
else{
[self.messageList insertMessageWith:msg];
}
}
}
}
}
+(MessageModel *)getMessageModel:(JMSGMessage *)jmessage WithServiceUser:(NSString *)serviceUser{
MessageModel *msg;
BOOL _isOutgoing;
UserModel *uModel;
JMSGUser *jUser;
NSString *strTime;
NSString *strInfo;
JMSGAbstractContent *content;
jUser = jmessage.fromUser;
content = jmessage.content;
strTime = [Utils formatDateToString:[jmessage.timestamp longLongValue]/1000 WithFormat:conversation_datetime_format];
uModel = [[UserModel alloc] init];
uModel.userId = [NSString stringWithFormat:@"%@",jUser.username];
uModel.displayName = jUser.nickname?jUser.nickname:jUser.noteName?jUser.noteName:jUser.username;
uModel.avatarUrlString = jUser.avatar;
//判断会话对象
if ([uModel.userId isEqualToString:serviceUser]) {
_isOutgoing = NO;//表示对方
uModel.Avatar = [UIImage imageNamed:@"user_service.png"];
}
else{
_isOutgoing = YES;//表示自己
uModel.Avatar = [UIImage imageNamed:@"user_pic.png"];
}
switch (jmessage.contentType) {
//语音消息
case kJMSGContentTypeVoice:
strInfo = [NSString stringWithFormat:@"%@",[((JMSGVoiceContent *) content) valueForKey:@"resourcePath"]];
msg = [[MessageModel alloc] initWithVoicePath:strInfo
duration:((JMSGVoiceContent *) content).duration.floatValue
messageId:jmessage.serverMessageId
fromUser:uModel
timeString:strTime
isOutgoing:_isOutgoing
status:IMUIMessageStatusSuccess];
break;
//视频消息
case kJMSGContentTypeVideo:
strInfo = [NSString stringWithFormat:@"%@",[((JMSGVideoContent *) content) valueForKey:@"resourcePath"]];
msg = [[MessageModel alloc] initWithVideoPath:strInfo
messageId:jmessage.serverMessageId
fromUser:uModel
timeString:strTime
isOutgoing:_isOutgoing
status:IMUIMessageStatusSuccess];
break;
//图片消息
case kJMSGContentTypeImage:
strInfo = [NSString stringWithFormat:@"%@",((JMSGImageContent *)content).imageLink];
if (strInfo && ![strInfo isEqualToString:@""] && ![strInfo isEqualToString:@"(null)"]) {
msg = [[MessageModel alloc] initWithImageUrl:strInfo.urlEncoded
messageId:jmessage.serverMessageId
fromUser:uModel
timeString:strTime
isOutgoing:_isOutgoing
status:IMUIMessageStatusSuccess];
}
else{
strInfo = [NSString stringWithFormat:@"%@",((JMSGImageContent *)content).thumbImageLocalPath];
if (!strInfo || [strInfo isEqualToString:@""] || [strInfo isEqualToString:@"(null)"]) {
strInfo = [NSString stringWithFormat:@"%@",((JMSGImageContent *)content).originMediaLocalPath];
}
msg = [[MessageModel alloc] initWithImagePath:strInfo
messageId:jmessage.serverMessageId
fromUser:uModel
timeString:strTime
isOutgoing:_isOutgoing
status:IMUIMessageStatusSuccess];
}
break;
//文本消息
case kJMSGContentTypeText:
strInfo = [NSString stringWithFormat:@"%@",((JMSGTextContent *) content).text];
msg = [[MessageModel alloc] initWithText:strInfo
messageId:jmessage.serverMessageId
fromUser:uModel
timeString:strTime
isOutgoing:_isOutgoing
status:IMUIMessageStatusSuccess];
break;
default:
NSLog(@"未知消息类型!详见:%ld",(long)jmessage.contentType);
break;
}
return msg;
}
//MARK: -
/** 获取消息编号 */
-(NSString *)getMessageId{
return [NSString stringWithFormat:@"%f",[[NSDate new] timeIntervalSince1970] * 1000];
}
//MARK: - 请求数据
/** 加载历史消息 */
-(void)requestLoadHistoryMessage:(BOOL)isLoading{
if (![TDUser shareInstance]) {
NSLog(@"用户数据不存在");
[self stopAnimation];
return;
}
if (!self.strServiceUserName || [self.strServiceUserName isEqualToString:@""]) {
[MBProgressHUD showError:@"请先指定单聊会话的对象"];
return;
}
//获取与该用户的会话的历史信息
[self loadMessageWithConversation:self.singleConversionm AndLoading:isLoading AndRefresh:NO];
}
-(void)stopAnimation{
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUDForView:self.view];
if (self.messageList.messageCollectionView.mj_header) {
[self.messageList.messageCollectionView.mj_header endRefreshing];
[self.messageList.messageCollectionView.mj_header setHidden:!self->isMoreData];
}
});
}
//MARK: - IMUIInputViewDelegate
- (void)messageCollectionView:(UICollectionView * _Nonnull)willBeginDragging {
[_imuiInputView hideFeatureView];
}
//MARK: - 发送文本信息
- (void)sendTextMessage:(NSString * _Nonnull)messageText {
MessageModel *message = [[MessageModel alloc] initWithText:messageText
messageId:[self getMessageId]
fromUser:self.currentUser
timeString:[Utils getCurrentDateToString:conversation_datetime_format]
isOutgoing:YES
status:IMUIMessageStatusSuccess];
[self.messageList appendMessageWith:message];
//极光IM发送文本消息
[JMSGMessage sendSingleTextMessage:messageText
toUser:self.strServiceUserName];
}
//MARK: - 发送语音消息
- (void)finishRecordVoice:(NSString * _Nonnull)voicePath durationTime:(double)durationTime {
MessageModel *message = [[MessageModel alloc] initWithVoicePath:voicePath
duration:durationTime
messageId:[self getMessageId]
fromUser:self.currentUser
timeString:[Utils getCurrentDateToString:conversation_datetime_format]
isOutgoing:YES
status:IMUIMessageStatusSuccess];
[_messageList appendMessageWith: message];
//极光IM发送语音消息
NSData *voiceData = [NSData dataWithContentsOfFile:voicePath];
[JMSGMessage sendSingleVoiceMessage:voiceData
voiceDuration:[NSNumber numberWithDouble:durationTime]
toUser:self.strServiceUserName];
}
//MARK: - 相册发送照片
- (void)didSeletedGalleryWithAssetArr:(NSArray<PHAsset *> * _Nonnull)AssetArr {
for (PHAsset *asset in AssetArr) {
switch (asset.mediaType) {
case PHAssetMediaTypeImage: {
PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
options.synchronous = YES;
[[PHImageManager defaultManager] requestImageForAsset: asset
targetSize: CGSizeMake(100.0, 100.0)
contentMode:PHImageContentModeAspectFill
options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
NSData *imageData = UIImagePNGRepresentation(result);
if(imageData){
if (![[NSFileManager defaultManager] fileExistsAtPath:K_APP_JIM_IMAGES_PATH]) {
[[NSFileManager defaultManager] createDirectoryAtPath:K_APP_JIM_IMAGES_PATH withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *imagePath = [NSString stringWithFormat:@"%@/%f",K_APP_JIM_IMAGES_PATH,NSDate.timeIntervalSinceReferenceDate];
if ([imageData writeToFile:imagePath atomically:YES]) {
MessageModel *message = [[MessageModel alloc]
initWithImagePath:imagePath
messageId:[self getMessageId]
fromUser:self.currentUser
timeString:[Utils getCurrentDateToString:conversation_datetime_format]
isOutgoing:YES
status:IMUIMessageStatusSuccess];
[self->_messageList appendMessageWith: message];
//极光发送图片消息
[JMSGMessage sendSingleImageMessage:imageData toUser:self.strServiceUserName];
}
}
}];
break;
}
default:
break;
}
}
}
//MARK: - 拍照发送图片
- (void)didShootPictureWithPicture:(NSData * _Nonnull)picture {
if(picture){
if (![[NSFileManager defaultManager] fileExistsAtPath:K_APP_JIM_IMAGES_PATH]) {
[[NSFileManager defaultManager] createDirectoryAtPath:K_APP_JIM_IMAGES_PATH withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *imagePath = [NSString stringWithFormat:@"%@/%f",K_APP_JIM_IMAGES_PATH,NSDate.timeIntervalSinceReferenceDate];
if ([picture writeToFile: imagePath atomically: true]) {
MessageModel *message = [[MessageModel alloc] initWithImagePath:imagePath
messageId:[self getMessageId]
fromUser:self.currentUser
timeString:[Utils getCurrentDateToString:conversation_datetime_format]
isOutgoing:YES
status:IMUIMessageStatusSuccess];
[_messageList appendMessageWith: message];
//极光发送图片消息
[JMSGMessage sendSingleImageMessage:picture toUser:self.strServiceUserName];
}
}
}
//MARK: - 发送视频消息
- (void)finishRecordVideoWithVideoPath:(NSString * _Nonnull)videoPath durationTime:(double)durationTime {
MessageModel *message = [[MessageModel alloc] initWithVideoPath:videoPath
messageId:[self getMessageId]
fromUser:self.currentUser
timeString:[Utils getCurrentDateToString:conversation_datetime_format]
isOutgoing:YES
status:IMUIMessageStatusSuccess];
[_messageList appendMessageWith: message];
// 视频格式,如:mp4、mov
NSString *strVideoFormat = [videoPath pathExtension];
NSData *videoData = [NSData dataWithContentsOfFile:videoPath];
//极光发送视频
[self.singleConversionm sendVideoMessage:videoData
thumbData:videoData
videoFormat:strVideoFormat
duration:[NSNumber numberWithDouble:durationTime]];
}
//MARK: - 数据展示
- (UICollectionViewCell * _Nullable)messageCollectionViewWithMessageCollectionView:(UICollectionView * _Nonnull)messageCollectionView forItemAt:(NSIndexPath * _Nonnull)forItemAt messageModel:(id <IMUIMessageProtocol> _Nonnull)messageModel SWIFT_WARN_UNUSED_RESULT {
if ([messageModel isKindOfClass: [MessageEventModel class]]) {
MessageEventCollectionViewCell *cell = [messageCollectionView dequeueReusableCellWithReuseIdentifier:[[MessageEventCollectionViewCell class] description] forIndexPath:forItemAt];
MessageEventModel *event = (MessageEventModel *)messageModel;
[cell presentCell: event.evenText];
return cell;
} else {
return nil;
}
}
-(void)messageCollectionViewWithDidTapStatusViewInCell:(UICollectionViewCell *)didTapStatusViewInCell model:(id<IMUIMessageProtocol>)model{
NSLog(@"阅读状态消息点击");
}
-(void)messageCollectionViewWithDidTapHeaderImageInCell:(UICollectionViewCell *)didTapHeaderImageInCell model:(id<IMUIMessageProtocol>)model{
NSLog(@"图像点击");
}
-(void)messageCollectionViewWithDidTapMessageBubbleInCell:(UICollectionViewCell *)didTapMessageBubbleInCell model:(id<IMUIMessageProtocol>)model{
NSLog(@"消息气泡点击");
}
- (NSNumber * _Nullable)messageCollectionViewWithMessageCollectionView:(UICollectionView * _Nonnull)messageCollectionView heightForItemAtIndexPath:(NSIndexPath * _Nonnull)forItemAt messageModel:(id <IMUIMessageProtocol> _Nonnull)messageModel SWIFT_WARN_UNUSED_RESULT {
if ([messageModel isKindOfClass: [MessageEventModel class]]) {
return @(20.0);
}
return nil;
}
//MARK: - IMUICustomInputViewDelegate
- (void)keyBoardWillShowWithHeight:(CGFloat)height durationTime:(double)durationTime {
[_messageList scrollToBottomWith: YES];
}
//MARK: - JMessageDelegate
//接收消息(服务器端下发的)回调
-(void)onReceiveMessage:(JMSGMessage *)message error:(NSError *)error{
NSLog(@"message:%@,error:%@",message,error.localizedDescription);
//当前会话页面收到消息,更新列表
if (!error) {
MessageModel *msg = [IMConversationViewController getMessageModel:message
WithServiceUser:self.strServiceUserName];
if(msg){
[self.messageList appendMessageWith:msg];
}
}
}
//MARK: - 键盘通知
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
//键盘高度(负数弹出键盘 正数收起键盘)
CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;
[UIView animateWithDuration:duration animations:^{
//收起键盘
if (yOffset > 21) {
[self->_imuiInputView hideFeatureView];
}
}];
}
@end
上面代码是使用Aurora IMUI 实现的聊天页面,具体功能包括:分页加载会话消息、发送图片、语音、视频、文本消息以及下拉加载更多消息(注意不是下拉刷新)
另外附上
以上就是这么多,有兴趣的可以给我留言交流(附上效果图,如下:)