融云 SDK 如何实现群组操作
背景
在集成融云 SDK 后,需要实现群组操作的消息通知。包括:
- 群创建、销毁通知
- 群公告通知
- 群人员加入、退出通知
- 群昵称修改通知
- ...
融云现有的 SDK 仅提供了 RCGroupNotificationMessage
,内部封装了几种简单的类型操作,且扩展性不强,无法完成现有的操作。
解决方案:
使用自定义消息重新来构建群组通知消息。
参考 sealtlak 中的 RCDGroupNotificationMessage
,也是单独对群组操作内容,重新进行了封装处理。
/*!
群组通知消息
*/
@interface RCDGroupNotificationMessage : RCMessageContent
//操作名
@property (nonatomic, copy) NSString *operation;
//操作人
@property (nonatomic, copy) NSString *operatorUserId;
//操作对象
@property (nonatomic, strong) NSArray<NSString *> *targetUserIds;
//内容
@property (nonatomic, copy) NSString *message;
//获取摘要
- (NSString *)getDigest:(NSString *)groupId;
@end
这个类预定义了下面几种操作类型消息,具体内容可参考名字定义
extern NSString *const RCDGroupCreate;
extern NSString *const RCDGroupMemberAdd;
extern NSString *const RCDGroupMemberQuit;
extern NSString *const RCDGroupMemberKicked;
extern NSString *const RCDGroupRename;
extern NSString *const RCDGroupBulletin;
extern NSString *const RCDGroupOwnerTransfer;
extern NSString *const RCDGroupDismiss;
extern NSString *const RCDGroupMemberJoin;
extern NSString *const RCDGroupMemberManagerSet;
extern NSString *const RCDGroupMemberManagerRemove;
extern NSString *const RCDGroupMemberProtectionOpen;
extern NSString *const RCDGroupMemberProtectionClose;
.m 的实现还是按照自定义消息的实现进行处理。唯一有区别的是在获取摘要的方法,在这个方法中对消息内容进行了处理,根据操作名的不同,来对显示的内容进行各种适配。此处根据根据自己的业务进行修改。
- (NSString *)getDigest:(NSString *)groupId {
NSString *content;
//获取用户信息
NSString *operationName = [self getDisplayNames:@[ self.operatorUserId?self.operatorUserId:@""] groupId:groupId];
NSString *targetNames = [self getDisplayNames:self.targetUserIds groupId:groupId];
//是否当前用户操作
BOOL isMeOperate = NO;
if ([self.operatorUserId isEqualToString:[RCIMClient sharedRCIMClient].currentUserInfo.userId]) {
isMeOperate = YES;
}
//判断操作类型
if ([self.operation isEqualToString:RCDGroupCreate]) {
content =
[NSString stringWithFormat:NSLocalizedStringFromTable(isMeOperate ? @"GroupHaveCreated" : @"GroupCreated",
@"RongCloudKit", nil),
operationName];
} else if ([self.operation isEqualToString:RCDGroupMemberAdd]) {
if (self.targetUserIds.count == 1 && [self.targetUserIds containsObject:self.operatorUserId]) {
content = [NSString
stringWithFormat:NSLocalizedStringFromTable(@"GroupJoin", @"RongCloudKit", nil), operationName];
} else {
content = [NSString
stringWithFormat:NSLocalizedStringFromTable(isMeOperate ? @"GroupHaveInvited" : @"GroupInvited",
@"RongCloudKit", nil),
operationName, targetNames];
}
} else if ([self.operation isEqualToString:RCDGroupMemberJoin]) {
content =
[NSString stringWithFormat:NSLocalizedStringFromTable(@"GroupJoin", @"RongCloudKit", nil), operationName];
} else if ([self.operation isEqualToString:RCDGroupMemberQuit]) {
content = [NSString stringWithFormat:NSLocalizedStringFromTable(isMeOperate ? @"GroupHaveQuit" : @"GroupQuit",
@"RongCloudKit", nil),
operationName];
} else if ([self.operation isEqualToString:RCDGroupMemberKicked]) {
content =
[NSString stringWithFormat:NSLocalizedStringFromTable(isMeOperate ? @"GroupHaveRemoved" : @"GroupRemoved",
@"RongCloudKit", nil),
operationName, targetNames];
} else if ([self.operation isEqualToString:RCDGroupRename]) {
content = [NSString stringWithFormat:NSLocalizedStringFromTable(@"GroupChanged", @"RongCloudKit", nil),
operationName, self.targetGroupName];
} else if ([self.operation isEqualToString:RCDGroupDismiss]) {
content =
[NSString stringWithFormat:NSLocalizedStringFromTable(isMeOperate ? @"GroupHaveDismiss" : @"GroupDismiss",
@"RongCloudKit", nil),
operationName];
} else if ([self.operation isEqualToString:RCDGroupOwnerTransfer]) {
content = [NSString stringWithFormat:RCDLocalizedString(@"GroupHasNewOwner"), targetNames];
} else if ([self.operation isEqualToString:RCDGroupMemberManagerSet]) {
content = [NSString stringWithFormat:RCDLocalizedString(@"GroupSetManagerMessage"), targetNames];
} else if ([self.operation isEqualToString:RCDGroupMemberProtectionOpen]) {
content = RCDLocalizedString(@"openMemberProtection");
} else if ([self.operation isEqualToString:RCDGroupMemberProtectionClose]) {
content = [NSString stringWithFormat:RCDLocalizedString(@"closeMemberProtection"), operationName];
} else {
content = NSLocalizedStringFromTable(@"unknown_message_cell_tip", @"RongCloudKit", nil);
}
return content;
}
此外,还有一个获取名称的方法,用来维护用户信息。
- (NSString *)getDisplayNames:(NSArray<NSString *> *)userIds groupId:(NSString *)groupId
网友评论