MOChatNotificationManager.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // MOChatNotificationManager.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/6/4.
  6. //
  7. #import "MOChatNotificationManager.h"
  8. #import "MOChatNotificationView.h"
  9. @interface MOChatNotificationManager () <MOChatNotificationViewDelegate>
  10. @property (nonatomic, strong) NSMutableArray<V2TIMMessage *> *notificationQueue;
  11. @property (nonatomic, strong) MOChatNotificationView *currentNotificationView;
  12. @property (nonatomic, assign) BOOL isShowingNotification;
  13. @property (nonatomic, copy) void(^notificationTapHandler)(V2TIMMessage *msg);
  14. @property (nonatomic, assign) BOOL notificationEnabled;
  15. @property (nonatomic, strong) dispatch_queue_t serialQueue; // 添加串行队列
  16. // 在 @interface MOChatNotificationManager () 部分添加
  17. @property (nonatomic, strong) NSMutableSet<NSNumber *> *currentScenes;
  18. @end
  19. @implementation MOChatNotificationManager
  20. - (void)dealloc {
  21. // 移除通知观察者
  22. [[NSNotificationCenter defaultCenter] removeObserver:self];
  23. }
  24. #pragma mark - 单例方法
  25. + (instancetype)sharedManager {
  26. static MOChatNotificationManager *sharedManager = nil;
  27. static dispatch_once_t onceToken;
  28. dispatch_once(&onceToken, ^{
  29. sharedManager = [[self alloc] init];
  30. });
  31. return sharedManager;
  32. }
  33. - (instancetype)init {
  34. self = [super init];
  35. if (self) {
  36. _notificationQueue = [NSMutableArray array];
  37. _isShowingNotification = NO;
  38. _notificationEnabled = YES; // 默认启用通知
  39. _serialQueue = dispatch_queue_create("com.app.notificationManager", DISPATCH_QUEUE_SERIAL);
  40. _currentScenes = [NSMutableSet set];
  41. // 注册应用生命周期通知
  42. [[NSNotificationCenter defaultCenter] addObserver:self
  43. selector:@selector(applicationDidEnterBackground:)
  44. name:UIApplicationDidEnterBackgroundNotification
  45. object:nil];
  46. [[NSNotificationCenter defaultCenter] addObserver:self
  47. selector:@selector(applicationWillEnterForeground:)
  48. name:UIApplicationWillEnterForegroundNotification
  49. object:nil];
  50. }
  51. return self;
  52. }
  53. #pragma mark - 公共方法
  54. // 添加场景管理方法
  55. - (void)enterScene:(MOChatNotificationScene)scene {
  56. dispatch_async(self.serialQueue, ^{
  57. [self.currentScenes addObject:@(scene)];
  58. [self updateNotificationEnabledState];
  59. });
  60. }
  61. - (void)leaveScene:(MOChatNotificationScene)scene {
  62. dispatch_async(self.serialQueue, ^{
  63. if ([self.currentScenes containsObject:@(scene)]) {
  64. [self.currentScenes removeObject:@(scene)];
  65. }
  66. [self updateNotificationEnabledState];
  67. });
  68. }
  69. - (void)updateNotificationEnabledState {
  70. BOOL shouldEnable = YES;
  71. // 检查当前是否有不允许显示通知的场景
  72. for (NSNumber *sceneNumber in self.currentScenes) {
  73. MOChatNotificationScene scene = [sceneNumber integerValue];
  74. if (scene >= MOChatNotificationSceneMessageList) { // 大于等于消息列表的场景都不显示通知
  75. shouldEnable = NO;
  76. break;
  77. }
  78. }
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. [self setNotificationEnabled:shouldEnable];
  81. });
  82. }
  83. - (void)showNotification:(V2TIMMessage *)notification{
  84. if (!notification) return;
  85. // 如果通知被禁用,则不显示
  86. if (!self.notificationEnabled) return;
  87. if(!(notification.elemType == V2TIM_ELEM_TYPE_TEXT ||
  88. notification.elemType == V2TIM_ELEM_TYPE_IMAGE ||
  89. notification.elemType == V2TIM_ELEM_TYPE_SOUND ||
  90. notification.elemType == V2TIM_ELEM_TYPE_VIDEO ||
  91. notification.elemType == V2TIM_ELEM_TYPE_FILE)) return;
  92. dispatch_async(self.serialQueue, ^{
  93. // 检查是否有来自同一聊天的消息
  94. BOOL foundSameChat = NO;
  95. NSInteger sameIndex = -1;
  96. for (NSInteger i = 0; i < self.notificationQueue.count; i++) {
  97. V2TIMMessage *existingNotification = self.notificationQueue[i];
  98. if ([existingNotification.sender isEqualToString:notification.sender]) {
  99. sameIndex = i;
  100. foundSameChat = YES;
  101. break;
  102. }
  103. }
  104. if (foundSameChat && sameIndex >= 0 && sameIndex < self.notificationQueue.count) {
  105. // 直接替换队列中的消息
  106. [self.notificationQueue replaceObjectAtIndex:sameIndex withObject:notification];
  107. } else {
  108. // 添加到队列
  109. if(self.currentNotificationView &&
  110. [self.currentNotificationView.model.sender isEqualToString:notification.sender]){
  111. // 如果当前显示的通知来自同一聊天,下面会直接更新它
  112. }
  113. else{
  114. [self.notificationQueue addObject:notification];
  115. }
  116. }
  117. // 检查当前显示的通知是否与新通知来自同一聊天,如果是则立即更新
  118. dispatch_async(dispatch_get_main_queue(), ^{
  119. if (self.currentNotificationView &&
  120. [self.currentNotificationView.model.sender isEqualToString:notification.sender]) {
  121. // 更新当前显示的通知内容
  122. self.currentNotificationView.model = notification;
  123. }
  124. // 如果当前没有显示通知,则开始显示
  125. else if (!self.isShowingNotification) {
  126. [self showNextNotification];
  127. }
  128. });
  129. });
  130. }
  131. - (void)clearAllNotifications {
  132. [self.notificationQueue removeAllObjects];
  133. if (self.currentNotificationView) {
  134. [self.currentNotificationView dismissWithAnimation];
  135. self.currentNotificationView = nil;
  136. }
  137. // 额外的安全措施:遍历并移除所有可能残留的通知视图
  138. [self removeAllNotificationViewsFromWindows];
  139. self.isShowingNotification = NO;
  140. }
  141. - (void)setupNotificationTapHandler:(void(^)(V2TIMMessage *msg))handler {
  142. self.notificationTapHandler = handler;
  143. }
  144. - (void)setNotificationEnabled:(BOOL)enabled {
  145. _notificationEnabled = enabled;
  146. // 如果禁用通知,则清除当前所有通知
  147. if (!enabled) {
  148. [self clearAllNotifications];
  149. // 确保所有通知视图都被移除
  150. [self removeAllNotificationViewsFromWindows];
  151. }
  152. }
  153. - (BOOL)isNotificationEnabled {
  154. return _notificationEnabled;
  155. }
  156. #pragma mark - 私有方法
  157. - (void)showNextNotification {
  158. // 如果通知被禁用,则不显示
  159. if (!self.notificationEnabled) return;
  160. if (self.notificationQueue.count == 0) {
  161. self.isShowingNotification = NO;
  162. return;
  163. }
  164. self.isShowingNotification = YES;
  165. // 获取队列中的第一条通知
  166. V2TIMMessage *notification = [self.notificationQueue firstObject];
  167. [self.notificationQueue removeObjectAtIndex:0];
  168. // 创建并显示通知视图
  169. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  170. self.currentNotificationView = [[MOChatNotificationView alloc] initWithModel:notification];
  171. self.currentNotificationView.delegate = self;
  172. [keyWindow addSubview:self.currentNotificationView];
  173. [self.currentNotificationView showWithAnimation];
  174. // 3秒后自动隐藏
  175. __weak typeof(self) weakSelf = self;
  176. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  177. [weakSelf hideCurrentNotification];
  178. });
  179. }
  180. - (void)hideCurrentNotification {
  181. if (self.currentNotificationView) {
  182. __weak typeof(self) weakSelf = self;
  183. MOChatNotificationView *viewToRemove = self.currentNotificationView;
  184. self.currentNotificationView = nil;
  185. // 使用动画移除视图
  186. [viewToRemove dismissWithAnimation];
  187. // 延迟一小段时间后显示下一条通知,避免动画重叠
  188. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  189. // 确保视图已被移除
  190. if ([viewToRemove superview]) {
  191. [viewToRemove removeFromSuperview];
  192. }
  193. [weakSelf showNextNotification];
  194. });
  195. }
  196. }
  197. #pragma mark - MOChatNotificationViewDelegate
  198. - (void)didTapNotificationWithChatId:(V2TIMMessage *)msg {
  199. // 点击通知时,隐藏当前通知并执行回调
  200. [self hideCurrentNotification];
  201. if (self.notificationTapHandler) {
  202. self.notificationTapHandler(msg);
  203. }
  204. }
  205. #pragma mark - 辅助方法
  206. - (void)removeAllNotificationViewsFromWindows {
  207. // 获取所有窗口
  208. NSArray *windows = [UIApplication sharedApplication].windows;
  209. for (UIWindow *window in windows) {
  210. // 递归查找并移除所有MOChatNotificationView
  211. [self removeNotificationViewsFromView:window];
  212. }
  213. }
  214. - (void)removeNotificationViewsFromView:(UIView *)view {
  215. // 检查当前视图是否为MOChatNotificationView
  216. if ([view isKindOfClass:[MOChatNotificationView class]]) {
  217. [view removeFromSuperview];
  218. return; // 如果当前视图已经是MOChatNotificationView,移除后直接返回,不需要继续遍历其子视图
  219. }
  220. // 创建子视图的副本,因为在遍历过程中可能会修改子视图数组
  221. NSArray *subviews = [view.subviews copy];
  222. for (UIView *subview in subviews) {
  223. [self removeNotificationViewsFromView:subview];
  224. }
  225. }
  226. #pragma mark - 应用生命周期处理
  227. - (void)applicationDidEnterBackground:(NSNotification *)notification {
  228. // 应用进入后台时,清理所有通知
  229. dispatch_async(dispatch_get_main_queue(), ^{
  230. // 直接移除当前通知视图,不执行动画
  231. if (self.currentNotificationView) {
  232. [self.currentNotificationView removeFromSuperview];
  233. self.currentNotificationView = nil;
  234. }
  235. // 遍历所有窗口,查找并移除所有MOChatNotificationView
  236. [self removeAllNotificationViewsFromWindows];
  237. // 清空队列
  238. [self.notificationQueue removeAllObjects];
  239. self.isShowingNotification = NO;
  240. });
  241. }
  242. - (void)applicationWillEnterForeground:(NSNotification *)notification {
  243. // 应用即将进入前台,重置状态
  244. dispatch_async(dispatch_get_main_queue(), ^{
  245. // 确保状态正确
  246. if (self.currentNotificationView) {
  247. [self.currentNotificationView removeFromSuperview];
  248. self.currentNotificationView = nil;
  249. }
  250. // 遍历所有窗口,查找并移除所有MOChatNotificationView
  251. [self removeAllNotificationViewsFromWindows];
  252. self.isShowingNotification = NO;
  253. // 如果有待显示的通知,则开始显示
  254. if (self.notificationQueue.count > 0 && self.notificationEnabled) {
  255. [self showNextNotification];
  256. }
  257. });
  258. }
  259. @end