美文网首页
腾讯云即时通讯(三)-----自定义消息

腾讯云即时通讯(三)-----自定义消息

作者: 进阶的蚊子 | 来源:发表于2017-08-22 20:50 被阅读840次

腾讯云IM的sdk中主要提供了 以下几个消息类

TIMTextElem  (文本消息)

TIMImageElem  (图片消息)

TIMFileElem  (文件消息)

TIMSoundElem (语音消息)

TIMLocationElem  (地理位置)

TIMFaceElem  (表情消息类型)

TIMVideoElem (微视频消息)

TIMUGCElem  (UGC视频)

一般来说这些消息类都可以满足我们的需求,然而产品和boss 才是老大,他们定方案了,苦逼的程序员还得费力去实现.

这个时候自定义消息就派的上用场了.我们先来来看看这个自定义的消息类  TIMCustomElem

/**

*  自定义消息类型

*/

@interface TIMCustomElem : TIMElem

/**

*  自定义消息二进制数据

*/

@property(nonatomic,strong) NSData * data;

/**

*  自定义消息描述信息,做离线Push时文本展示(已废弃,请使用TIMMessage中offlinePushInfo进行配置)

*/

@property(nonatomic,strong) NSString * desc DEPRECATED_ATTRIBUTE;

/**

*  离线Push时扩展字段信息(已废弃,请使用TIMMessage中offlinePushInfo进行配置)

*/

@property(nonatomic,strong) NSString * ext DEPRECATED_ATTRIBUTE;

/**

*  离线Push时声音字段信息(已废弃,请使用TIMMessage中offlinePushInfo进行配置)

*/

@property(nonatomic,strong) NSString * sound DEPRECATED_ATTRIBUTE;

@end

注释已经很明显 主要是有个属性 是NSData 类型的  ,就是我们需要把自定义的消息封装转化成NSData传递过去.

楼主的需求中是需要自定义个消息,展示订单信息,需要显示下单者的联系方式 ,地址信息 收货人 ,商品图片,商品价格,商品名等信息

构造过程

NSMutableDictionary* mesDict=[NSMutableDictionary new];

  [mesDict setObject:@"订单描述" forKey:KTIMCustomGoodsDescp];

  [mesDict setObject:@"南京橙子哈哈哈啊哈哈哈哈哈 啊哈哈哈哈哈哈哈哈" forKey:KTIMCustomGoodsName];

  [mesDict setObject:@"75.00" forKey:KTIMCustomGoodsPrice];

    [mesDict setObject:@"101" forKey:KTIMCustomGoodsType];

    [mesDict setObject:@"https://imgs.nanniwan.com/upload/nnw/avatar/2017/08/14/1502701925339.jpg" forKey:KTIMCustomGoodsImage];

  [mesDict setObject:@"2" forKey:KTIMCustomGoodsNum];

    [mesDict setObject:@"张三" forKey:KTIMCustomGoodsReceiver];

    [mesDict setObject:@"13869746765" forKey:KTIMCustomGoodsMobile];

    [mesDict setObject:@"湖北省武汉市江夏区光谷科技港栋9楼1220" forKey:KTIMCustomGoodsAddress];

// 转换为NSData

NSData* data=[NSJSONSerialization dataWithJSONObject:mesDict options:NSJSONWritingPrettyPrinted error:nil];

TIMCustomElem * custom_elem = [[TIMCustomElem alloc] init];

[custom_elem setData:data];

TIMMessage * msg = [[TIMMessage alloc] init];

[msg addElem:custom_elem];

构造后的消息体形式为

data={

"mobile" : "13869746765",

"goods_image" : "https:\/\/imgs.nanniwan.com\/upload\/nnw\/avatar\/2017\/08\/14\/1502701925339.jpg",

"address" : "湖北省武汉市江夏区光谷科技港栋9楼1220",

"receiver" : "张三",

"goods_price" : "75.00",

"type" : "101",

"goods_name" : "南京橙子哈哈哈啊哈哈哈哈哈 啊哈哈哈哈哈哈哈哈",

"goods_descp" : "订单描述",

"goods_num" : "2"

}, desc=, ext=

//消息构成后我们需要把消息发送出去  调用一下这个方法 先获取会话

/**

*  获取会话

*

*  @param type 会话类型,TIM_C2C 表示单聊 TIM_GROUP 表示群聊

*              TIM_SYSTEM 表示系统会话

*  @param receiver C2C 为对方用户 identifier,GROUP 为群组Id,SYSTEM为@""

*

*  @return 会话对象

*/

- (TIMConversation*)getConversation:(TIMConversationType)type receiver:(NSString*)receiver;

TIMConversation * conversation = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:receiver];

//掺入消息

[conversation sendMessage:msg succ:^(){

NSLog(@"消息插入成功");

}

}fail:^(int code, NSString * err) {

NSLog(@"消息插入失败");

}];

这样自定义的消息插入就成功了,但是要把自定义的消息正确显示在界面上还要自定义一个消息显示的cell类

我们在 ChatTableViewCell增加一个类ChatCustomOrderTableViewCell 让它继承于ChatBaseTableViewCell

@interface ChatCustomOrderTableViewCell : ChatBaseTableViewCell

{

@protected

UILabel    *_descLabel;//商品描述

UILabel    *_goodsNameLabel;//商品名

UILabel    *_goodsPriceLabel;//商品价格

UILabel    *_goodsNumLabel;//商品数量

UIImageView    *_goodsImage;//商品图片

UILabel    *_seperateLabel;//分割线

UILabel    *_contactNameLabel;//联系人

UILabel    *_phoneLabel;//电话号码

UILabel    *_addressLabel;//地址信息

}

@end

//然后实现该这个类

//商品名

#define KTIMCustomGoodsName @"goods_name"

//图片名

#define KTIMCustomGoodsImage @"goods_image"

//价格名

#define KTIMCustomGoodsPrice @"goods_price"

//描述

#define KTIMCustomGoodsDescp @"goods_drsp"

//类型

#define KTIMCustomGoodsType @"type"

////自定订单义消息字段

//数量

#define KTIMCustomGoodsNum @"goods_num"

//联系人

#define KTIMCustomGoodsReceiver @"receiver"

//联系人电话

#define KTIMCustomGoodsMobile @"mobile"

//联系人电话

#define KTIMCustomGoodsAddress @"address"

@implementation  ChatCustomOrderTableViewCell

// 只创建,外部统一添加

- (UIView *)addElemContent

{

_elemContentRef=[[UIView alloc]init];

_descLabel=[[UILabel alloc]init];

_descLabel.font=FONT(14);

_descLabel.textColor=COLOR_HEX(@"#333333");

_descLabel.textAlignment= NSTextAlignmentLeft ;

[_elemContentRef addSubview:_descLabel];

_goodsNameLabel=[[UILabel alloc]init];

_goodsNameLabel.font=FONT(13);

_goodsNameLabel.numberOfLines=2;

_goodsNameLabel.textColor=COLOR_HEX(@"#333333");

_goodsNameLabel.textAlignment= NSTextAlignmentLeft ;

[_elemContentRef addSubview:_goodsNameLabel];

_goodsPriceLabel=[[UILabel alloc]init];

_goodsPriceLabel.font=FONT(12);

_goodsPriceLabel.textColor=COLOR_HEX(@"#eb6100");

_goodsPriceLabel.textAlignment= NSTextAlignmentLeft ;

[_elemContentRef addSubview:_goodsPriceLabel];

_goodsImage=[[UIImageView alloc]init];

[_elemContentRef addSubview:_goodsImage];

_seperateLabel=[[UILabel alloc]init];

_seperateLabel.backgroundColor=COLOR_HEX(@"#efefef");

[_elemContentRef addSubview:_seperateLabel];

_contactNameLabel=[[UILabel alloc]init];

_contactNameLabel.font=FONT(12);

_contactNameLabel.textColor=COLOR_HEX(@"#333333");

_contactNameLabel.textAlignment= NSTextAlignmentLeft ;

[_elemContentRef addSubview:_contactNameLabel];

_phoneLabel=[[UILabel alloc]init];

_phoneLabel.font=FONT(12);

_phoneLabel.textColor=COLOR_HEX(@"#333333");

_phoneLabel.textAlignment= NSTextAlignmentLeft ;

[_elemContentRef addSubview:_phoneLabel];

_addressLabel=[[UILabel alloc]init];

_addressLabel.font=FONT(12);

_addressLabel.textColor=COLOR_HEX(@"#333333");

_addressLabel.numberOfLines=2;

_addressLabel.textAlignment= NSTextAlignmentLeft ;

[_elemContentRef addSubview:_addressLabel];

//添加约束

[self keepDistanse];

[self configContent];

return _elemContentRef;

}

//设置约束

-(void)keepDistanse

{

//mark 全部要已_elemContentRef为基准 不然不正确

//设置约束

CGFloat imgW=60;

CGFloat imgH=60;

[_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_elemContentRef.top).offset(6);

make.left.mas_equalTo(_elemContentRef.left).offset(15);

make.right.mas_equalTo(_elemContentRef.right).offset(-15);

make.height.mas_equalTo(@16);

}];

[_goodsImage mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.mas_equalTo(_elemContentRef.left).offset(15);

make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16);

make.width.mas_equalTo(imgW);

make.height.mas_equalTo(imgH);

}];

[_goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16);

make.left.mas_equalTo(_elemContentRef.left).offset(8+imgW+15);

make.right.mas_equalTo(_elemContentRef.right).offset(-15);

make.height.lessThanOrEqualTo(@36);

}];

[_goodsPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH-16);

make.left.mas_equalTo(_elemContentRef.left).offset(8+imgW+15);

make.height.mas_equalTo(@15);

make.right.mas_equalTo(_elemContentRef.right).offset(-15);

}];

[_seperateLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8);

make.left.mas_equalTo(_elemContentRef.left).offset(+15);

make.height.mas_equalTo(@0.5);

make.right.mas_equalTo(_elemContentRef.right).offset(-15);

}];

[_contactNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8+8.5);

make.left.mas_equalTo(_elemContentRef.left).offset(+15);

make.height.mas_equalTo(@14);

make.right.mas_equalTo(_elemContentRef.right).offset(-12);

}];

[_phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8+8.5+14);

make.left.mas_equalTo(_elemContentRef.left).offset(+15);

make.height.mas_equalTo(@14);

make.right.mas_equalTo(_elemContentRef.right).offset(-12);

}];

[_addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.mas_equalTo(_elemContentRef.top).offset(6+6+16+imgH+8+8.5+14+14);

make.left.mas_equalTo(_elemContentRef.left).offset(+15);

make.height.lessThanOrEqualTo(@32);

make.right.mas_equalTo(_elemContentRef.right).offset(-12);

}];

}

- (void)configContent

{

[super configContent];

TIMMessage* message=_msg.msg;

TIMElem* item=[message getElem:0];

if([item isKindOfClass:[TIMCustomElem class]]){

TIMCustomElem* elem=(TIMCustomElem*)item;

NSData* data=elem.data;

NSDictionary* dict= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

//给各个控件赋值

//_descLabel.text=dict[KTIMCustomGoodsDescp];

_descLabel.text=@"订单描述";

[_goodsImage sd_setImageWithURL:[NSURL URLWithString:dict[KTIMCustomGoodsImage] ] placeholderImage:PLACEHOLDER];

_goodsNameLabel.text=dict[KTIMCustomGoodsName];

NSString* string=[NSString stringWithFormat:@"共%@件商品 ¥%@",dict[KTIMCustomGoodsNum],dict[KTIMCustomGoodsPrice]];

NSString* price=[NSString stringWithFormat:@"¥%@",dict[KTIMCustomGoodsPrice]];

NSArray* attary=@[[ConfigAttributedString foregroundColor:COLOR_HEX(@"#333333") range:NSMakeRange(0, string.length-price.length)],[ConfigAttributedString foregroundColor:COLOR_HEX(@"#eb6100") range:NSMakeRange(string.length-price.length, price.length)]];

_goodsPriceLabel.attributedText=[string createAttributedStringAndConfig:attary];

//_goodsPriceLabel.text=[NSString stringWithFormat:@"共%@件商品 ¥%@",dict[KTIMCustomGoodsNum],dict[KTIMCustomGoodsPrice]];

_contactNameLabel.text=[NSString stringWithFormat:@"联系人: %@",dict[KTIMCustomGoodsReceiver]];

// _addressLabel.text=dict[KTIMCustomGoodsAddress];

_addressLabel.text=[NSString stringWithFormat:@"电话: %@",dict[KTIMCustomGoodsAddress]];

_phoneLabel.text=[NSString stringWithFormat:@"地址: %@",dict[KTIMCustomGoodsMobile]];

}

}

//遇到的坑是之前设置约束时使用相对于兄弟控件来布局一直不正确,后来采用这种死板的布局才达到要求。。。

相关文章

  • 腾讯云即时通讯(三)-----自定义消息

    腾讯云IM的sdk中主要提供了 以下几个消息类 TIMTextElem (文本消息) TIMImageElem (...

  • 腾讯云通讯IM 接入小程序 (三)发送表情

    微信小程序利用腾讯云IM即时通讯表情开发 上一节我们完成了消息收发 这次我们补充表情发送;其实表情跟普通文字消息发...

  • 腾讯云 自定义消息展示

    最近在开发的过程中,有个需求就是自定义消息具体的需求看设计图效果,下方放出,并且记录一下开发过程,仅供学习参考 1...

  • 主流互联网第三方服务商

    即时通讯 「融云」 「环信」 「网易云信」 推送 「极光推送」、「阿里云」、「个推」、「腾讯云」 支付 「微信支...

  • 环信SDK 自定义扩展消息

    项目中集成了环信即时通讯,但是项目需要自定义消息类型,看环信发送消息方法,在发送消息的时候可以附加自定义的内容。 ...

  • 腾讯云即时通讯离线消息集成总结

    前言 最近公司的聊天消息增加一个离线消息推送的功能,之前因为排期的问题,只是简单的本地通知,在账号离线的状态下或者...

  • 即时聊天

    六个免费开源的即时通讯软件源代码 常见即时通讯:网易IM 、腾讯IM、环信IM、融云IM、leancloud IM...

  • 腾讯云批量部署服务器环境的方法

    腾讯云服务器批量部署环境需要用到腾讯云自带的自定义镜像功能,大致方法:先将部署好环境的云服务器制作自定义镜像,再通...

  • 基于XMPP构建的iOS即时通讯项目

    前言: 项目中经常用到环信、融云、腾讯云的即时通讯的第三方的SDK,自己也很好奇这些SDK内部的实现原理,自己闲来...

  • 腾讯云IM 聊天发送自定义消息

    发送自定义消息官网也讲的很清楚 自定义消息是指当内置的消息类型无法满足特殊需求,开发者可以自定义消息格式,内容全部...

网友评论

      本文标题:腾讯云即时通讯(三)-----自定义消息

      本文链接:https://www.haomeiwen.com/subject/rlxrdxtx.html