消息收发
单聊指的是一对一的收发消息。云通讯SDK支持一对一发送文本消息、图片消息、文件消息、位置消息、富文本消息。代码示例详细介绍如下,参考Demo中IMChattingHelper类和ChattingFragment类)。
API介绍
1、ECMessage是消息基类,ECMessageBody是抽象的消息内容类,可查看iOS消息类结构。
参考类:ECMessage查看API
2、发送消息。
- /**
- @brief 发送消息
- @discussion 发送文本消息时,进度不生效;发送附件消息时,进度代理生效
- @param message 发送的消息
- @param progress 发送进度代理
- @param completion 执行结果回调block
- @return 函数调用成功返回消息id,失败返回nil
- */
- -(NSString*)sendMessage:(ECMessage*)message progress:(id<ECProgressDelegate>)progress completion:(void(^)(ECError *error, ECMessage* message))completion;
参考类:ECChatManager查看API
发送文本
代码示例
- //1. 构造文本消息体
- ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWithText:@"你好,欢迎来到云通讯"];
- //2. 构造出具体消息,设置消息接收者、对应的消息体
- //发送单聊消息,接收者传对方的登录账号;发送群组消息,接收者传群组id
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- //4. 消息发送
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){ //禁言处理
- //您已被群组禁言
- }else{//其他错误处理
- //发送失败
- }
- }];
- 说明:
- /**如果需要跨应用发送消息,需通过appkey+英文井号+用户帐号的方式拼接,发送录音、发送群组消息等与此方式一致;群组跨应用只是邀请成员时,被邀请的用户要用appid#账号的格式,向群组里发消息不需要。//
- 例如:appkey=20150314000000110000000000000010
- 帐号ID=john
- 传入帐号=20150314000000110000000000000010#john
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"appkey#John的账号Id" body:messageBody];*/
发送图片
代码示例
- //1. 构造图片消息体
- ECImageMessageBody *messageBody = [[ECImageMessageBody alloc] initWithFile:@"图片文件本地绝对路径"
- displayName:@"文件名称"];
- //2. 构造出具体消息,设置消息接收者、对应的消息体
- //发送单聊消息,接收者传对方的登录账号;发送群组消息,接收者传群组id
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- #warning 取本地时间
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {
- //发送成功
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid)
- {
- //您已被群组禁言
- }else{
- //发送失败
- }}];
发送附件
代码示例
- //1. 构造文件消息体
- ECFileMessageBody *messageBody = [[ECFileMessageBody alloc] initWithFile:@"文件本地绝对路径"
- displayName:@"文件名称"];
- //2. 构造出具体消息,设置消息接收者、对应的消息体
- //发送单聊消息,接收者传对方的登录账号;发送群组消息,接收者传群组id
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- #warning 取本地时间
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {
- //发送成功
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid)
- {
- //您已被群组禁言
- }else{
- //发送失败
- }}];
发送语音
代码示例
- //1. 构造语音消息体
- ECVoiceMessageBody *messageBody =[[ECVoiceMessageBody alloc] initWithFile:"语音文件路径.arm"
- displayName:@"文件名.amr"];
- //2.开始录音
- [[ECDevice sharedInstance].messageManager startVoiceRecording:messageBody error:^(ECError *error,
- ECVoiceMessageBody *messageBody) {
- if (error.errorCode == ECErrorType_RecordTimeOut) {//录音超时,立即发送;应用也可以选择不发送
- //3.构造出具体消息,设置消息接收者、对应的消息体
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //4. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- //5.消息发送
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil
- completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {//发消息送成功回调
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode ==
- ECErrorType_File_Have_Forbid){//禁言处理
- //您已被群组禁言
- }else{//发送失败,其他错误处理
- }
- }];
- }
- }];
- //1.停止录音
- [[ECDevice sharedInstance].messageManager stopVoiceRecording:^(ECError *error, ECVoiceMessageBody
- *messageBody) {
- if (error.errorCode == ECErrorType_NoError) {
- //2.构造出具体消息,设置消息接收者、对应的消息体
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- //4.消息发送
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil
- completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {
- //发送成功
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode ==
- ECErrorType_File_Have_Forbid){
- //您已被群组禁言
- }else{
- //发送失败
- }
- }];
- } else if (error.errorCode == ECErrorType_RecordTimeTooShort) {
- //录音时间过短
- }
- }];
发送变声消息
代码示例
- ECSountTouchConfig *config = [[ECSountTouchConfig alloc] init];
- config.pitch = 8;
- config.pitch = 0;
- config.rate = -20;
- config.srcVoice = srcFile;
- config.dstVoice = desFile;
- [[ECDevice sharedInstance].messageManager changeVoiceWithSoundConfig:config
- completion:^(ECError *error, ECSountTouchConfig* dstSoundConfig) {
- ECVoiceMessageBody * messageBody = [[ECVoiceMessageBody alloc] initWithFile:[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)
- objectAtIndex:0] stringByAppendingPathComponent:[dstSoundConfig.dstVoice
- lastPathComponent]] displayName:[dstSoundConfig.dstVoice lastPathComponent]];
- [[ECDevice sharedInstance].messageManager playVoiceMessage:messageBody completion:^(ECError *error) {
- }];}参照demo
发送短视频消息
代码示例
- //1. 构造视频消息体
- ECVideoMessageBody *messageBody = [[ECVideoMessageBody alloc] initWithFile:@"文件本地绝对路径" displayName:@"文件名称"];
- //2. 构造出具体消息,设置消息接收者、对应的消息体
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- //4. 消息发送
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){//禁言处理
- //您已被群组禁言
- }else{//其他错误处理
- //发送失败
- }
- }];
发送地理位置
我们假设John收到Tony单聊时,john发送给tony一条位置消息,tony点击会自动跳转并点击右上角导航跳转苹果地图为您导航,则代码如下:
说明:关于发送地理位置,该功能需要用户自己在客户端通过发送消息的接口实现,SDK中没有封装具体的发送位置接口(android、ios均需用户自己在客户端实现)。
- //1. 构造位置消息体
- CLLocationCoordinate2D location = CLLocationCoordinate2DMake(0, 0);//待发送的位置
- ECLocationMessageBody *messageBody = [[ECLocationMessageBody alloc] initWithCoordinate:location andTitle:@"地点标题"];
- //2. 构造出具体消息,设置消息接收者、对应的消息体
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- //4. 消息发送
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){//禁言处理
- //您已被群组禁言
- }else{//其他错误处理
- //发送失败
- }
- }];
发送富文本消息
发送富文本消息是点击链接进入后,可将本网页的内容抓取后分享到云通讯平台的单人和群组讨论组,代码如下:
- //1.构造消息体
- ECPreviewMessageBody *messageBody = [[ECPreviewMessageBody alloc] initWithFile:”本地文件路径” displayName: ”本地文件路径后缀名”];
- messageBody.url = ”链接消息URL”;
- messageBody.title = ”网页的表头”;
- messageBody.remotePath = ”网页抓取图片的地址”;
- messageBody.desc = ”网页抓取的描述”;
- messageBody.thumbnailLocalPath = ”图片缩略图路径”;
- //2. 构造出具体消息,设置消息接收者、对应的消息体
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
- NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
- //3. 设置消息时间(发送消息是本地时间,接收消息是服务器时间)
- message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
- //4. 消息发送
- [ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error,
- ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {
- //发送成功
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid)
- {
- //您已被群组禁言
- }else {}}];
发送状态消息
当A收到B的第一条消息后,B如果还在输入,则A能获取B的输入状态,代码如下:
- //1. 构造用户现状态消息体
- ECUserStateMessageBody *messageBody = [[ECUserStateMessageBody alloc] initWithUserState:[NSString stringWithFormat:@"%d", ECUserInputState_White]];
- //2. 构造出具体消息,设置消息接收者、对应的消息体
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"" body:messageBody];
- //3. 发送消息
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {// 消息发送成功回调处理
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){//禁言处理
- //您已被群组禁言
- }else{//发送失败,其他错误处理
- }
- }];
SDK中预定了3种常用的用户状态
- typedef NS_ENUM(NSInteger, ECUserInputState) {
- ECUserInputState_None,
- ECUserInputState_White,//正在输入…
- ECUserInputState_Record //正在录音
- };
自定义消息
自定义消息通过发送消息接口扩展字段来实现,具体说明如下:
- ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWithText:@"你好,欢迎来到云通讯"];
- ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
- message.userData = @"扩展字段";
- //发送同上
接收消息
我们假设John收到Tony发送过来的消息,则代码如下:
- /**
- @该通知回调接口在代理类里面
- @brief 接收即时消息代理函数
- @param message 接收的消息
- */-(void)onReceiveMessage:(ECMessage*)message{
- NSLog:(@"收到%@的消息,属于%@会话", message.from, message.sessionId);switch(message.messageBody.messageBodyType){
- case MessageBodyType_Text:{
- ECTextMessageBody *msgBody = (ECTextMessageBody *)message.messageBody;
- NSLog(@"收到的是文本消息------%@,msgBody.text");
- break;
- }
- case MessageBodyType_Voice:{
- ECVoiceMessageBody *msgBody = (ECVoiceMessageBody *)message.messageBody;
- NSLog(@"音频文件remote路径------%@",msgBody. remotePath);
- break;
- }
- case MessageBodyType_Video:{
- ECVideoMessageBody *msgBody = (ECVideoMessageBody *)message.messageBody;
- NSLog(@"视频文件remote路径------%@",msgBody. remotePath);
- break;
- }
- case MessageBodyType_Image:{
- ECImageMessageBody *msgBody = (ECImageMessageBody *)message.messageBody;
- NSLog(@"图片文件remote路径------%@",msgBody. remotePath);
- NSLog(@"缩略图片文件remote路径------%@",msgBody. thumbnailRemotePath);
- break;
- }
- case MessageBodyType_File:{
- ECFileMessageBody *msgBody = (ECFileMessageBody *)message.messageBody;
- NSLog(@"文件remote路径------%@",msgBody. remotePath);
- break;
- }
- default:
- break;}}