为了方便不同用户的定制化及业务需求,IMSDK 目前提供了五个维度的自定义字段
"用户资料自定义字段","好友自定义字段","群成员维度自定义字段","群维度自定义字段" 均已键值对 Key-Value 形式存储并使用。扩展相应的字段 Key,为相应的字段 Key 设置值 Value,这便是 IMSDK 字段的使用方式
"消息自定义字段" 有些不一样,请翻至对应的章节详阅
控制台添加自定义字段
1)进入控制台打开 "应用配置"
2)切换至 "功能配置" 页
3)将会看到 "用户资料自定义字段","好友自定义字段","群成员维度自定义字段","群维度自定义字段"
4)点击 "+ 新增自定义字段" 配置 "字段名称 Key" 和 "读写权限"
字段名只能由字母、数字、下划线(_)组成,不能以数字开头,且长度不能超过 16 个字符,一旦添加成功后,便不可修改及删除
读写权限可再次编辑
安卓示例请参考
用户资料自定义字段
/// 设置自己的资料
- (int)modifySelfProfile:(NSDictionary<NSString \*, id> \*)values succ:(TIMSucc)succ fail:(TIMFail)fail;
/// 获取自己的资料
- (int)getSelfProfile:(TIMGetProfileSucc)succ fail:(TIMFail)fail;
///获取指定用户资料
- (int)getUsersProfile:(NSArray<NSString \*> \*)identifiers forceUpdate:(BOOL)forceUpdate succ:(TIMUserProfileArraySucc)succ fail:(TIMFail)fail;
/// 在缓存中查询用户的资料
- (TIMUserProfile \*)queryUserProfile:(NSString \*)identifier;
/// 在缓存中查询自己的资料
- (TIMUserProfile \*)querySelfProfile;
{
NSString \*customKey = [TIMProfileTypeKey\_Custom\_Prefix stringByAppendingString:@"Test"];;
NSString \*customValue = @"TestValue";
NSDictionary \*dict = @{TIMProfileTypeKey\_Nick : @"Jack", customKey:customValue};
[[TIMFriendshipManager sharedInstance] modifySelfProfile:dict succ:^{
NSLog(@"succ");
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);;
}];;
}
{
[[TIMFriendshipManager sharedInstance] getSelfProfile:^(TIMUserProfile \*profile) {
for (NSString \*key in profile.customInfo) {
NSData \*value = profile.customInfo[key];
NSString\* stringValue = [[NSString alloc] initWithData:value encoding:NSASCIIStringEncoding];
NSLog(@"keyandvalue = %@,%@", key, stringValue);
}
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);
}];
}
key是NSString类型,value是NSData类型或者NSNumber类型, key值按照后台配置的字符串传入,不包括 TIMProfileTypeKey_Custom_Prefix 前缀
/// 修改好友
- (int)modifyFriend:(NSString \*)identifier values:(NSDictionary<NSString \*, id> \*)values succ:(TIMSucc)succ fail:(TIMFail)fail;
/// 获取好友列表, 返回好友(TIMFriend)列表
-(int)getFriendList:(TIMFriendArraySucc)succ fail:(TIMFail)fail;
/// 在缓存中查询用户的关系链数据
- (TIMFriend \*)queryFriend:(NSString \*)identifier
// 获取缓存中的关系链列表
- (NSArray<TIMFriend \*> \*)queryFriendList;
添加好友成功后,可以对好友进行备注,标记好友来源等字段可供使用,某些特殊场景下需要用到自定义字段,例如:星标好友,好友亲密度等一些场景
NSString \*customKey = [TIMFriendTypeKey\_Custom\_Prefix stringByAppendingString:@"Test"];
NSString \*customValue = @"TestValue";
NSDictionary \*dict = @{customKey:customValue};
NSString \* friendIdentifier = @"user3000";
[[TIMFriendshipManager sharedInstance] modifyFriend:friendIdentifier values:dict succ:^{
NSLog(@"succ");
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);
}];
[[TIMFriendshipManager sharedInstance] getFriendList:^(NSArray<TIMFriend \*> \*friends) {
for (TIMFriend \* friend in friends) {
for (NSString \*key in friend.customInfo) {
NSData \*value = friend.customInfo[key];
NSString\* stringValue = [[NSString alloc] initWithData:value encoding:NSASCIIStringEncoding];
NSLog(@"keyandvalue = %@,%@", key, stringValue);
}
}
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);
}];
自定义字段集合,key是NSString类型,value是NSData类型或者NSNumber类型, key值按照后台配置的字符串传入,不包括 TIMFriendTypeKey_Custom_Prefix 前缀
/// 修改群成员自定义字段集合
- (int)modifyGroupMemberInfoSetCustomInfo:(NSString\*)groupId user:(NSString\*)identifier customInfo:(NSDictionary<NSString\*,NSData\*> \*)customInfo succ:(TIMSucc)succ fail:(TIMFail)fail;
/// 获取群组指定成员的信息
- (int)getGroupMembersInfo:(NSString\*)groupId members:(NSArray<NSString \*>\*)members succ:(TIMGroupMemberSucc)succ fail:(TIMFail)fail;
///获取群成员列表
- (int)getGroupMembers:(NSString\*)groupId succ:(TIMGroupMemberSucc)succ fail:(TIMFail)fail;
NSString\* value = @"TestValue";
NSData \* customdata = [value dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary \*dict = @{@"Test":customdata};
[[TIMGroupManager sharedInstance] modifyGroupMemberInfoSetCustomInfo:@"@TGS#2RYFSRXFV" user:@"user3000" customInfo:dict succ:^{
NSLog(@"succ");
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);
}];
[[TIMGroupManager sharedInstance] getGroupMembersInfo:@"@TGS#2RYFSRXFV" members:@[@"user3000"] succ:^(NSArray \*members) {
for (TIMGroupMemberInfo \*MemberInfo in members) {
for (NSString \*key in MemberInfo.customInfo) {
NSData \*value = MemberInfo.customInfo[key];
NSString\* stringValue = [[NSString alloc] initWithData:value encoding:NSASCIIStringEncoding];
NSLog(@"keyandvalue = %@,%@", key, stringValue);
}
}
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);
}];
/// 修改群自定义字段集合
- (int)modifyGroupCustomInfo:(NSString\*)groupId customInfo:(NSDictionary<NSString \*,NSData \*>\*)customInfo succ:(TIMSucc)succ fail:(TIMFail)fail;
/// 获取服务器存储的群组信息
- (int)getGroupInfo:(NSArray\*)groups succ:(TIMGroupListSucc)succ fail:(TIMFail)fail;
///获取本地存储的群组信息
- (TIMGroupInfo \*)queryGroupInfo:(NSString \*)groupId;
NSString\* value = @"TestValue";
NSData \* customdata = [value dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary \*dict = @{@"imtest":customdata};
[[TIMGroupManager sharedInstance] modifyGroupCustomInfo:@"@TGS#2RYFSRXFV" customInfo:dict succ:^{
NSLog(@"succ");
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);
}];
[[TIMGroupManager sharedInstance] getGroupInfo:@[@"@TGS#2RYFSRXFV"] succ:^(NSArray \*groupList) {
for (TIMGroupInfo \*info in groupList) {
for (NSString \*key in info.customInfo) {
NSData \*value = info.customInfo[key];
NSString\* stringValue = [[NSString alloc] initWithData:value encoding:NSASCIIStringEncoding];
NSLog(@"keyandvalue = %@,%@", key, stringValue);
}
}
} fail:^(int code, NSString \*msg) {
NSLog(@"fail: code: %d, msg: %@", code, msg);
}];
消息的自定义字段信息有两种,例如 Int 可以像微信一样标记语音消息是否播放过,0 为未播放,显示红点,1 为播放过了,不显示红点。Str 可以给消息分类,将开发者或用户的一些字段进行设置,再通过字段过滤某些消息
/// 设置自定义整数,默认为 0
- (BOOL)setCustomInt:(int32\_t)param;
/// 获取 CustomInt
- (int32\_t)customInt;
///设置自定义数据,默认为""
- (BOOL)setCustomData:(NSData\*)data;
///获取 CustomData
- (NSData\*)customData;
// 设置
imMsg.customInt = 1;
imMsg.customData = [@"string" dataUsingEncoding:NSUTF8StringEncoding];
// 获取
NSLog(@"%d,%@", imMsg.customInt, [[NSString alloc] initWithData:imMsg.customData encoding:NSASCIIStringEncoding]);
TIMMessage 对象只会返回 getSenderNickname, 不能直接获取自定义字段, 在会话中不建议获取自定义字段, 一条消息就去拿会很频繁,给后台造成很大的压力
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。