IM 即时通讯 (439)
Android (207)
- 初始化&链接 (8)
- 事件&监听处理 (7)
- 用户信息 (13)
- 会话列表 (15)
- 聊天会话 (26)
- 消息处理 (35)
- 自定义消息 (6)
- 音视频 (2)
- 推送&通知 (33)
- 扩展功能 (4)
- 第三方地图 (3)
- 依赖&配置 (9)
- 升级说明 (3)
- 其他 (43)
iOS (167)
- SDK 导入 (9)
- 连接 (8)
- 事件处理 (2)
- 用户信息 (3)
- 会话列表 (14)
- 聊天会话 (44)
- 消息处理 (25)
- 自定义消息 (8)
- 推送&通知 (19)
- 扩展功能 (5)
- 国际化 (3)
- 音视频 (4)
- 其他 (23)
Web (36)
Server (29)
在创建自定义消息时设置 senderUserInfo及mentionedInfo。
回答:
需要在自定义消息的m文件中对senderUserInfo进行编解码操作,可模仿下面代码实现:
- (NSData *)encode {
NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
[dataDict setObject:self.content forKey:@"content"];
if (self.extra) {
[dataDict setObject:self.extra forKey:@"extra"];
}
if (self.senderUserInfo) {
NSMutableDictionary *__dic = [[NSMutableDictionary alloc] init];
if (self.senderUserInfo.name) {
[__dic setObject:self.senderUserInfo.name forKeyedSubscript:@"name"];
}
if (self.senderUserInfo.portraitUri) {
[__dic setObject:self.senderUserInfo.portraitUri forKeyedSubscript:@"portrait"];
}
if (self.senderUserInfo.userId) {
[__dic setObject:self.senderUserInfo.userId forKeyedSubscript:@"id"];
}
[dataDict setObject:__dic forKey:@"user"];
}
if (self.mentionedInfo) {
NSMutableDictionary *mentionedInfodic = [[NSMutableDictionary alloc] init];
[mentionedInfodic setObject:@(self.mentionedInfo.type) forKeyedSubscript:@"type"];
if (self.mentionedInfo.type == RC_Mentioned_Users) {
[mentionedInfodic setObject:self.mentionedInfo.userIdList forKeyedSubscript:@"userIdList"];
}
[mentionedInfodic setObject:self.mentionedInfo.mentionedContent forKeyedSubscript:@"mentionedContent"];
[dataDict setObject:mentionedInfodic forKey:@"mentionedInfo"];
}
// NSDictionary* dataDict = [NSDictionary
// dictionaryWithObjectsAndKeys:self.content, @"content", nil];
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict options:kNilOptions error:nil];
return data;
}
- (void)decodeWithData:(NSData *)data {
__autoreleasing NSError *__error = nil;
if (!data) {
return;
}
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&__error];
RCDictionary *json = [[RCDictionary alloc] initWithDictionary:dictionary];
if (json) {
self.content = [json stringObjectForKey:@"content"];
self.extra = [json stringObjectForKey:@"extra"];
NSDictionary *userinfoDic = [json objectForKey:@"user"];
[self decodeUserInfo:userinfoDic];
NSDictionary *mentionedInfoDic = [json objectForKey:@"mentionedInfo"];
[self decodeMentionedInfo:mentionedInfoDic];
}
}
(RCDictionary只是为了方便得到字符串类型的数据,可以忽略这个封装的类,自己实现之后的赋值就可以了。)