相对于环信的自定义消息,融云提供的自定义消息更为简便。以下面红包消息为例.

首先针对自定义消息类型,你需要创建你的消息模型,必须继承于RCMessageContent,你可以根据环信官方demo中的事例去实现这个消息模型。
假设创建一个HongBaoMessage
.HongBaoMessage.h
#define BYHongBaotMessageTypeIdentifier @"RCD:HongBaoMsg"
@interface HongBaoMessage : RCMessageContent<NSCoding>
@property(nonatomic, strong) NSString *money;
@property(nonatomic, strong) NSString *extra;
+ (instancetype)messageWithContent:(NSString *)money;
HongBaoMessage.m
///初始化
+ (instancetype)messageWithContent:(NSString *)money {
HongBaoMessage *text = [[HongBaoMessage alloc] init];
if (text) {
text.money = money;
}
return text;
}
///消息是否存储,是否计入未读数
+ (RCMessagePersistent)persistentFlag {
return (MessagePersistent_ISPERSISTED | MessagePersistent_ISCOUNTED);
}
/// NSCoding
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.money = [aDecoder decodeObjectForKey:@"content"];
self.extra = [aDecoder decodeObjectForKey:@"extra"];
}
return self;
}
/// NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.money forKey:@"money"];
[aCoder encodeObject:self.extra forKey:@"extra"];
}
///将消息内容编码成json
- (NSData *)encode {
NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
[dataDict setObject:self.money forKey:@"money"];
if (self.extra) {
[dataDict setObject:self.extra forKey:@"extra"];
}
if (self.senderUserInfo) {
NSMutableDictionary *userInfoDic = [[NSMutableDictionary alloc] init];
if (self.senderUserInfo.name) {
[userInfoDic setObject:self.senderUserInfo.name forKeyedSubscript:@"name"];
}
if (self.senderUserInfo.portraitUri) {
[userInfoDic setObject:self.senderUserInfo.portraitUri forKeyedSubscript:@"portrait"];
}
if (self.senderUserInfo.userId) {
[userInfoDic setObject:self.senderUserInfo.userId forKeyedSubscript:@"id"];
}
[dataDict setObject:userInfoDic forKey:@"user"];
}
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];
return data;
}
///将json解码生成消息内容
- (void)decodeWithData:(NSData *)data {
if (data) {
__autoreleasing NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (dictionary) {
self.money = dictionary[@"money"];
self.extra = dictionary[@"extra"];
NSDictionary *userinfoDic = dictionary[@"user"];
[self decodeUserInfo:userinfoDic];
}
}
}
/// 会话列表中显示的摘要
- (NSString *)conversationDigest {
return self.money;
}
///消息的类型名
+ (NSString *)getObjectName {
return BYHongBaotMessageTypeIdentifier;
}
这样就完成了HongBaoMessage。接下来是自定义cell 的创建
IMKit 消息 cell 都继承自 RCMessageBaseCell ,RCMessageCell 是在继承 RCMessageBaseCell 的基础上增加显示头像和昵称。 自定义消息 cell 可以根据业务场景选择继承于 RCMessageCell 或者 RCMessageBaseCell。
RCMessageCell 结构图:

RCMessageBaseCell 结构图:

我们创建一个基于RCMessageCell 的HongBaoCell,这样就不用去处理头像和昵称的布局,只需要在messageContentView上添加我们自定义的view即可。
需要注意的是必须重写一下方法返回cell的宽高。
/*!
自定义消息 Cell 的 Size
@param model 要显示的消息model
@param collectionViewWidth cell所在的collectionView的宽度
@param extraHeight cell内容区域之外的高度
@return 自定义消息Cell的Size
@discussion 当应用自定义消息时,必须实现该方法来返回cell的Size。
其中,extraHeight是Cell根据界面上下文,需要额外显示的高度(比如时间、用户名的高度等)。
一般而言,Cell的高度应该是内容显示的高度再加上extraHeight的高度。
*/
+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
withCollectionViewWidth:(CGFloat)collectionViewWidth
referenceExtraHeight:(CGFloat)extraHeight;
核心代码就是布局了,在setData下面去处理布局
- (void)setDataModel:(RCMessageModel *)model {
[super setDataModel:model];
[self setAutoLayout];
}
处理好发送和接收方view上的布局即可
- (void)setAutoLayout {
HongBaoMessage *mes = (HongBaoMessage *)self.model.content;
if (mes) {
self.moneyLab.text = mes.money;
}
//发送方
if (MessageDirection_RECEIVE == self.messageDirection) {
}else {//接收方
}
}
接下来就是使用了,首先必须注册自定义消息message
//自定义消息
[[RCIM sharedRCIM] registerMessageType:[HongBaoMessage class]];
然后在会话页面 注册自定义cell
[self registerClass:[HongBaoCell class] forMessageClass:[ HongBaoMessage class]];
发送消息
- (void)sendmess {
HongBaoMessage *mes = [[HongBaoMessage alloc] init];
mes.money = @"4000";
mes.extra = @"红包未接受";
[[RCIM sharedRCIM] sendMessage:ConversationType_PRIVATE targetId:self.targetId content:mes pushContent:nil pushData:nil success:^(long messageId) {
} error:^(RCErrorCode nErrorCode, long messageId) {
}];
}

网友评论