diff --git a/PublicRoomMessageForward_Implementation.md b/PublicRoomMessageForward_Implementation.md new file mode 100644 index 00000000..94e1b659 --- /dev/null +++ b/PublicRoomMessageForward_Implementation.md @@ -0,0 +1,86 @@ +# 公共房间消息转发功能实现 + +## 功能概述 + +实现了从 PublicRoomManager 转发特定消息到房间中的功能。当 PublicRoomManager 接收到 attachment.first 为 106 的消息时,会自动转发到当前活跃的房间中。 + +## 实现方案 + +### 1. 通知机制 +- 使用 NSNotificationCenter 进行消息转发 +- 通知名称:`@"MessageFromPublicRoomWithAttachmentNotification"` +- 通知对象:NIMMessage 对象 + +### 2. 修改的文件 + +#### PublicRoomManager.m +- 在 `onRecvMessages:` 方法中添加转发逻辑 +- 当检测到 `attachment.first == 106` 时发送通知 + +#### XPRoomViewController.m +- 在 `setupNotifications` 方法中注册通知监听 +- 添加 `handlePublicRoomMessageForward:` 方法处理转发的消息 +- 在 `dealloc` 中自动移除通知监听 + +#### YUMIConstant.m +- 添加常量定义:`kMessageFromPublicRoomWithAttachmentNotification`(已添加但当前使用字符串字面量) + +#### XPRoomViewController.h +- 添加常量声明(已添加但当前使用字符串字面量) + +## 使用流程 + +1. **消息接收**:PublicRoomManager 接收到公共房间消息 +2. **类型检查**:检查 attachment.first 是否为 106 +3. **发送通知**:如果是 106 类型,发送转发通知 +4. **接收处理**:XPRoomViewController 接收通知并处理 +5. **消息显示**:通过现有的消息处理流程显示在房间中 + +## 代码示例 + +### 发送通知(PublicRoomManager.m) +```objective-c +if (attachment && attachment.first == 106) { + [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageFromPublicRoomWithAttachmentNotification" + object:message]; + NSLog(@"PublicRoomManager: 转发106类型消息到房间"); +} +``` + +### 接收处理(XPRoomViewController.m) +```objective-c +- (void)handlePublicRoomMessageForward:(NSNotification *)notification { + NIMMessage *message = notification.object; + if (![message isKindOfClass:[NIMMessage class]]) { + return; + } + + // 检查房间是否处于活跃状态 + if (!self.roomInfo || !self.messageContainerView) { + return; + } + + // 使用现有的消息处理流程 + [self.messageContainerView handleNIMCustomMessage:message]; +} +``` + +## 测试场景 + +1. **正常转发**:公共房间收到106类型消息时正确转发 +2. **房间状态**:房间最小化、关闭等状态下的处理 +3. **消息过滤**:确保转发的消息经过正确的过滤流程 +4. **性能影响**:确保不影响现有消息处理性能 + +## 注意事项 + +1. 消息会经过现有的 `isCanDisplayMessage` 过滤 +2. 支持最小化房间的特殊处理 +3. 自动处理内存管理(在 dealloc 中移除监听) +4. 包含完整的错误检查和日志记录 + +## 扩展性 + +如果将来需要转发其他类型的消息,可以: +1. 修改条件判断(如 `attachment.first == 107`) +2. 或者使用更通用的通知名称,在通知数据中携带消息类型信息 diff --git a/YuMi/Global/YUMIConstant.h b/YuMi/Global/YUMIConstant.h index cd4412d0..6402ad16 100644 --- a/YuMi/Global/YUMIConstant.h +++ b/YuMi/Global/YUMIConstant.h @@ -18,6 +18,7 @@ UIKIT_EXTERN NSString * const kRedPacketHistory; UIKIT_EXTERN NSString * const kTuWenMessageHistory;///图文消息已读记录 UIKIT_EXTERN NSString * const kRoomQuickMessageCloseCount; UIKIT_EXTERN NSString * const kLoginMethod; +UIKIT_EXTERN NSString * const kMessageFromPublicRoomWithAttachmentNotification; typedef NS_ENUM(NSUInteger, Pi_KeyType) { KeyType_PasswordEncode,///密码 des 加密的 KeyType_TRTC,///TRTC key diff --git a/YuMi/Global/YUMIConstant.m b/YuMi/Global/YUMIConstant.m index a318ea48..6283fe11 100644 --- a/YuMi/Global/YUMIConstant.m +++ b/YuMi/Global/YUMIConstant.m @@ -43,6 +43,7 @@ NSString * const kRoomRoomLittleGameMiniStageNotificationKey = @"kRoomRoomLittle NSString * const kFreeGiftCountdownNotification = @"kFreeGiftCountdownNotification";///免费礼物倒计时 +NSString * const kMessageFromPublicRoomWithAttachmentNotification = @"MessageFromPublicRoomWithAttachmentNotification";///公共房间消息转发通知 ///在里面进行判断当前环境是什么 NSString * const KeyWithType(Pi_KeyType type) { diff --git a/YuMi/Modules/YMRoom/Manager/PublicRoomManager.m b/YuMi/Modules/YMRoom/Manager/PublicRoomManager.m index 39abf585..5e240ac0 100644 --- a/YuMi/Modules/YMRoom/Manager/PublicRoomManager.m +++ b/YuMi/Modules/YMRoom/Manager/PublicRoomManager.m @@ -10,6 +10,8 @@ #import "ClientConfig.h" #import "AccountInfoStorage.h" #import "XPMessageRemoteExtModel.h" +#import "AttachmentModel.h" +#import "YUMIConstant.h" @interface PublicRoomManager () @@ -336,7 +338,33 @@ if (message.session.sessionType == NIMSessionTypeChatroom) { NSString *sessionId = message.session.sessionId; if ([sessionId isEqualToString:self.currentPublicRoomId]) { - NSLog(@"PublicRoomManager: 收到公共房间消息: %@", message.rawAttachContent); + NIMMessageChatroomExtension *messageExt = (NIMMessageChatroomExtension *)message.messageExt; +// NSDictionary * dic = [(NSDictionary *)messageExt.roomExt.toJSONObject objectForKey:message.from]; +// XPMessageRemoteExtModel *extModel = [XPMessageRemoteExtModel modelWithJSON:dic]; + + NSString * messageText = message.text; + AttachmentModel *attachment; + if (message.messageType == NIMMessageTypeCustom) { + NIMCustomObject *obj = (NIMCustomObject *) message.messageObject; + attachment = (AttachmentModel *) obj.attachment; + if (attachment.first == CustomMessageType_Car_Notify) { + if (attachment.second == Custom_Message_Sub_Car_OutDate) { + messageText = attachment.data[@"msg"]; + } + } + + // 检查是否为需要转发的消息类型 (attachment.first == 106) + if (attachment && attachment.first == 106) { + // 发送转发通知 + [[NSNotificationCenter defaultCenter] postNotificationName:@"MessageFromPublicRoomWithAttachmentNotification" + object:message]; + NSLog(@"PublicRoomManager: 转发106类型消息到房间"); + } + } + + NSLog(@"PublicRoomManager: 收到公共房间消息: %@\n%@", + message.rawAttachContent, + messageExt.roomExt); // 这里可以添加公共房间消息的处理逻辑 // TODO: 全服广播转移到此类 } diff --git a/YuMi/Modules/YMRoom/View/XPRoomViewController.h b/YuMi/Modules/YMRoom/View/XPRoomViewController.h index fc336a33..190f9256 100644 --- a/YuMi/Modules/YMRoom/View/XPRoomViewController.h +++ b/YuMi/Modules/YMRoom/View/XPRoomViewController.h @@ -7,6 +7,10 @@ #import "MvpViewController.h" #import "XPMessageRemoteExtModel.h" + +// 公共房间消息转发通知名称 +UIKIT_EXTERN NSString * const kMessageFromPublicRoomWithAttachmentNotification; + NS_ASSUME_NONNULL_BEGIN @interface XPRoomViewController : MvpViewController diff --git a/YuMi/Modules/YMRoom/View/XPRoomViewController.m b/YuMi/Modules/YMRoom/View/XPRoomViewController.m index dca28a73..1454930d 100644 --- a/YuMi/Modules/YMRoom/View/XPRoomViewController.m +++ b/YuMi/Modules/YMRoom/View/XPRoomViewController.m @@ -98,6 +98,7 @@ UIKIT_EXTERN NSString * const kRoomGiftEffectUpdateNotificationKey; UIKIT_EXTERN NSString * const kRoomMiniNotificationKey; UIKIT_EXTERN NSString * kNewUserRechargeKey; +UIKIT_EXTERN NSString * const kMessageFromPublicRoomWithAttachmentNotification; UIKIT_EXTERN NSString * const kFreeGiftCountdownNotification; NSString * const kHadShowAnchorRoomTipKey = @"kHadShowAnchorRoomTipKey";//是否展示过个播房上划用户引导 NSString * const kHadQuitOtherRoomKey = @"kHadQuitOtherRoomKey";//是否退出过非自己房间 @@ -421,6 +422,12 @@ XPCandyTreeInsufficientBalanceViewDelegate> [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myGiftEffectUpdate:) name:kRoomGiftEffectUpdateNotificationKey object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openRedPacketNotification:) name:@"kOpenRedPacketNotification" object:nil]; + // 添加公共房间消息转发通知监听 + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(handlePublicRoomMessageForward:) + name:@"MessageFromPublicRoomWithAttachmentNotification" + object:nil]; + // 添加应用生命周期保护 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) @@ -3121,4 +3128,26 @@ XPCandyTreeInsufficientBalanceViewDelegate> } return _redPacketView; } + +#pragma mark - 公共房间消息转发处理 + +- (void)handlePublicRoomMessageForward:(NSNotification *)notification { + NIMMessage *message = notification.object; + if (![message isKindOfClass:[NIMMessage class]]) { + NSLog(@"XPRoomViewController: 收到无效的公共房间转发消息"); + return; + } + + // 检查房间是否处于活跃状态 + if (!self.roomInfo || !self.messageContainerView) { + NSLog(@"XPRoomViewController: 房间未就绪,忽略公共房间转发消息"); + return; + } + + // 使用现有的消息处理流程 + [self.messageContainerView handleNIMCustomMessage:message]; + + NSLog(@"XPRoomViewController: 处理公共房间转发的106类型消息"); +} + @end