消息收发

单聊指的是一对一的收发消息。云通讯SDK支持一对一发送文本消息、图片消息、文件消息、位置消息、富文本消息。代码示例详细介绍如下,参考Demo中IMChattingHelper类和ChattingFragment类)。
API介绍
1、ECMessage是消息基类,ECMessageBody是抽象的消息内容类,可查看iOS消息类结构。
参考类:ECMessage查看API
2、发送消息。
  1. /**
  2.  @brief 发送消息
  3.  @discussion 发送文本消息时,进度不生效;发送附件消息时,进度代理生效
  4.  @param message 发送的消息
  5.  @param progress 发送进度代理
  6.  @param completion 执行结果回调block
  7.  @return 函数调用成功返回消息id,失败返回nil
  8.  */
  9. -(NSString*)sendMessage:(ECMessage*)message progress:(id<ECProgressDelegate>)progress completion:(void(^)(ECError *error, ECMessage* message))completion;
参考类:ECChatManager查看API

发送文本

代码示例
  1. //1. 构造文本消息体  
  2. ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWithText:@"你好,欢迎来到云通讯"];  
  3. //2. 构造出具体消息,设置消息接收者、对应的消息体 
  4. //发送单聊消息,接收者传对方的登录账号;发送群组消息,接收者传群组id
  5. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];  
  6. NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];  
  7. NSTimeInterval tmp =[date timeIntervalSince1970]*1000;  
  8. //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)  
  9. message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];  
  10. //4. 消息发送  
  11. [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {  
  12.    if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理  
  13.    }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){   //禁言处理  
  14.    //您已被群组禁言  
  15.    }else{//其他错误处理  
  16.      //发送失败  
  17.    }  
  18. }];  
  19.     
  20. 说明:
  21. /**如果需要跨应用发送消息,需通过appkey+英文井号+用户帐号的方式拼接,发送录音、发送群组消息等与此方式一致;群组跨应用只是邀请成员时,被邀请的用户要用appid#账号的格式,向群组里发消息不需要。//
  22. 例如:appkey=20150314000000110000000000000010 
  23. 帐号ID=john  
  24. 传入帐号=20150314000000110000000000000010#john             
  25. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"appkey#John的账号Id" body:messageBody];*/

发送图片

代码示例
  1. //1. 构造图片消息体
  2. ECImageMessageBody *messageBody = [[ECImageMessageBody alloc] initWithFile:@"图片文件本地绝对路径" 
  3. displayName:@"文件名称"];
  4. //2. 构造出具体消息,设置消息接收者、对应的消息体 
  5. //发送单聊消息,接收者传对方的登录账号;发送群组消息,接收者传群组id
  6. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
  7. #warning 取本地时间
  8. NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
  9. NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
  10. //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)  
  11. message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
  12. [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {  
  13.     if (error.errorCode == ECErrorType_NoError) {
  14.         //发送成功
  15.     }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid)
  16.     {
  17.         //您已被群组禁言
  18.     }else{
  19.     //发送失败
  20.     }}];

发送附件

代码示例
  1. //1. 构造文件消息体
  2. ECFileMessageBody *messageBody = [[ECFileMessageBody alloc] initWithFile:@"文件本地绝对路径" 
  3. displayName:@"文件名称"];
  4. //2. 构造出具体消息,设置消息接收者、对应的消息体 
  5. //发送单聊消息,接收者传对方的登录账号;发送群组消息,接收者传群组id
  6. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
  7. #warning 取本地时间
  8. NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
  9. NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
  10. //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)  
  11. message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
  12. [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {  
  13.     if (error.errorCode == ECErrorType_NoError) {
  14.         //发送成功
  15.     }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid)
  16.     {
  17.         //您已被群组禁言
  18.     }else{
  19.     //发送失败
  20.     }}];

发送语音

代码示例
  1. //1. 构造语音消息体
  2. ECVoiceMessageBody *messageBody =[[ECVoiceMessageBody alloc] initWithFile:"语音文件路径.arm"
  3.     displayName:@"文件名.amr"];    
  4. //2.开始录音
  5. [[ECDevice sharedInstance].messageManager startVoiceRecording:messageBody error:^(ECError *error, 
  6.     ECVoiceMessageBody *messageBody) {
  7.         if (error.errorCode == ECErrorType_RecordTimeOut) {//录音超时,立即发送;应用也可以选择不发送
  8.             //3.构造出具体消息,设置消息接收者、对应的消息体
  9.             ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody]; 
  10.             NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
  11.             NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
  12.             //4. 设置消息时间(发送消息是本地时间,接收消息是服务器时间) 
  13.             message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
  14.             //5.消息发送
  15.             [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil 
  16.             completion:^(ECError *error, ECMessage *amessage) {               
  17.                 if (error.errorCode == ECErrorType_NoError) {//发消息送成功回调
  18.                     
  19.                 }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == 
  20.                 ECErrorType_File_Have_Forbid){//禁言处理
  21.                     //您已被群组禁言
  22.                 }else{//发送失败,其他错误处理
  23.                     
  24.                 }
  25.             }];
  26.         }
  27. }];
  28.  
  29. //1.停止录音
  30. [[ECDevice sharedInstance].messageManager stopVoiceRecording:^(ECError *error, ECVoiceMessageBody 
  31.     *messageBody) {
  32.         if (error.errorCode == ECErrorType_NoError) { 
  33.             //2.构造出具体消息,设置消息接收者、对应的消息体    
  34.             ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
  35.             NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
  36.             NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
  37.             //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间) 
  38.             message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
  39.             //4.消息发送
  40.             [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil 
  41.             completion:^(ECError *error, ECMessage *amessage) {               
  42.                 if (error.errorCode == ECErrorType_NoError) {
  43.                     //发送成功
  44.                 }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == 
  45.                 ECErrorType_File_Have_Forbid){
  46.                     //您已被群组禁言
  47.                 }else{
  48.                     //发送失败
  49.                 }
  50.             }];
  51.         } else if  (error.errorCode == ECErrorType_RecordTimeTooShort) {
  52.             //录音时间过短
  53.         }
  54.     }];

发送变声消息

代码示例
  1. ECSountTouchConfig *config = [[ECSountTouchConfig alloc] init];
  2. config.pitch = 8;
  3. config.pitch = 0;
  4. config.rate = -20;
  5. config.srcVoice = srcFile;
  6. config.dstVoice = desFile;
  7. [[ECDevice sharedInstance].messageManager changeVoiceWithSoundConfig:config 
  8. completion:^(ECError *error, ECSountTouchConfig* dstSoundConfig) {
  9. ECVoiceMessageBody * messageBody = [[ECVoiceMessageBody alloc] initWithFile:[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) 
  10. objectAtIndex:0] stringByAppendingPathComponent:[dstSoundConfig.dstVoice 
  11. lastPathComponent]] displayName:[dstSoundConfig.dstVoice lastPathComponent]];
  12.  
  13. [[ECDevice sharedInstance].messageManager playVoiceMessage:messageBody completion:^(ECError *error) {
  14. }];}参照demo

发送短视频消息

代码示例
  1. //1. 构造视频消息体  
  2. ECVideoMessageBody *messageBody = [[ECVideoMessageBody alloc] initWithFile:@"文件本地绝对路径" displayName:@"文件名称"];  
  3. //2. 构造出具体消息,设置消息接收者、对应的消息体  
  4. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];  
  5. NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];  
  6. NSTimeInterval tmp =[date timeIntervalSince1970]*1000;  
  7. //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)  
  8. message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];  
  9. //4. 消息发送  
  10. [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {  
  11.     if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理  
  12.     }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){//禁言处理  
  13.        //您已被群组禁言  
  14.     }else{//其他错误处理  
  15.        //发送失败  
  16.     }  
  17.  }];  

发送地理位置

我们假设John收到Tony单聊时,john发送给tony一条位置消息,tony点击会自动跳转并点击右上角导航跳转苹果地图为您导航,则代码如下:
                
说明:关于发送地理位置,该功能需要用户自己在客户端通过发送消息的接口实现,SDK中没有封装具体的发送位置接口(android、ios均需用户自己在客户端实现)。
                     
  1. //1. 构造位置消息体  
  2. CLLocationCoordinate2D location = CLLocationCoordinate2DMake(0, 0);//待发送的位置  
  3. ECLocationMessageBody *messageBody = [[ECLocationMessageBody alloc] initWithCoordinate:location andTitle:@"地点标题"];  
  4. //2. 构造出具体消息,设置消息接收者、对应的消息体  
  5. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];  
  6. NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];  
  7. NSTimeInterval tmp =[date timeIntervalSince1970]*1000;  
  8. //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)  
  9. message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];  
  10. //4. 消息发送  
  11. [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {  
  12.    if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理  
  13.    }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){//禁言处理  
  14.       //您已被群组禁言  
  15.    }else{//其他错误处理  
  16.     //发送失败  
  17.    }  
  18.  }];               

发送富文本消息

发送富文本消息是点击链接进入后,可将本网页的内容抓取后分享到云通讯平台的单人和群组讨论组,代码如下:               
  1. //1.构造消息体
  2. ECPreviewMessageBody *messageBody = [[ECPreviewMessageBody alloc] initWithFile:”本地文件路径” displayName: ”本地文件路径后缀名”];
  3. messageBody.url = ”链接消息URL”;
  4. messageBody.title = ”网页的表头”;
  5. messageBody.remotePath = ”网页抓取图片的地址”;
  6. messageBody.desc = ”网页抓取的描述”;
  7. messageBody.thumbnailLocalPath = ”图片缩略图路径”;
  8. //2. 构造出具体消息,设置消息接收者、对应的消息体  
  9. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
  10. NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];  
  11. NSTimeInterval tmp =[date timeIntervalSince1970]*1000;  
  12. //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)  
  13. message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];  
  14. //4. 消息发送  
  15. [ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, 
  16.     ECMessage *amessage) {
  17.     
  18.     if (error.errorCode == ECErrorType_NoError) {
  19.         //发送成功
  20.     }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid)
  21.     {
  22.         //您已被群组禁言
  23.     }else {}}];

发送状态消息

当A收到B的第一条消息后,B如果还在输入,则A能获取B的输入状态,代码如下:                   
  1. //1. 构造用户现状态消息体  
  2. ECUserStateMessageBody *messageBody = [[ECUserStateMessageBody alloc] initWithUserState:[NSString stringWithFormat:@"%d", ECUserInputState_White]];  
  3. //2. 构造出具体消息,设置消息接收者、对应的消息体  
  4. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"" body:messageBody];  
  5. //3. 发送消息  
  6. [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {  
  7.    if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理  
  8.    }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){//禁言处理  
  9.      //您已被群组禁言  
  10.    }else{//发送失败,其他错误处理  
  11.    }  
  12.  }]; 
SDK中预定于了3种常用的用户状态
  1. typedef NS_ENUM(NSInteger, ECUserInputState) {  
  2.     ECUserInputState_None,  
  3.     ECUserInputState_White,//正在输入…  
  4.     ECUserInputState_Record  //正在录音
  5. }; 

自定义消息

自定义消息通过发送消息接口扩展字段来实现,具体说明如下:                                        
  1. ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWithText:@"你好,欢迎来到云通讯"];
  2. ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
  3. message.userData = @"扩展字段";
  4. //发送同上      

接收消息

我们假设John收到Tony发送过来的消息,则代码如下:                     
  1. /**
  2.  @该通知回调接口在代理类里面
  3.  @brief 接收即时消息代理函数
  4.  @param message 接收的消息
  5.  */-(void)onReceiveMessage:(ECMessage*)message{
  6.    NSLog:(@"收到%@的消息,属于%@会话", message.from, message.sessionId);switch(message.messageBody.messageBodyType){
  7.    case MessageBodyType_Text:{
  8.      ECTextMessageBody *msgBody = (ECTextMessageBody *)message.messageBody;
  9.      NSLog(@"收到的是文本消息------%@,msgBody.text");
  10.      break;
  11.    }
  12.    case MessageBodyType_Voice:{
  13.      ECVoiceMessageBody *msgBody = (ECVoiceMessageBody *)message.messageBody;
  14.      NSLog(@"音频文件remote路径------%@",msgBody. remotePath);
  15.      break;
  16.    }
  17.    case MessageBodyType_Video:{
  18.      ECVideoMessageBody *msgBody = (ECVideoMessageBody *)message.messageBody;
  19.      NSLog(@"视频文件remote路径------%@",msgBody. remotePath);
  20.      break;
  21.    }
  22.    case MessageBodyType_Image:{
  23.      ECImageMessageBody *msgBody = (ECImageMessageBody *)message.messageBody;
  24.      NSLog(@"图片文件remote路径------%@",msgBody. remotePath);
  25.      NSLog(@"缩略图片文件remote路径------%@",msgBody. thumbnailRemotePath);
  26.      break;
  27.    }
  28.    case MessageBodyType_File:{
  29.      ECFileMessageBody *msgBody = (ECFileMessageBody *)message.messageBody;
  30.      NSLog(@"文件remote路径------%@",msgBody. remotePath);
  31.      break;
  32.    }
  33.    default:
  34.      break;}}