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

腾讯云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]];

}

}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,305评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,751评论 0 1
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,144评论 1 4
  • 町宝阅读 134评论 0 0
  • I had a dream, before I met you, I was introduced to some...
    fangny14阅读 146评论 0 0