| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- //
- // MOChatNotificationManager.m
- // MiMoLive
- //
- // Created by SuperC on 2025/6/4.
- //
- #import "MOChatNotificationManager.h"
- #import "MOChatNotificationView.h"
- @interface MOChatNotificationManager () <MOChatNotificationViewDelegate>
- @property (nonatomic, strong) NSMutableArray<V2TIMMessage *> *notificationQueue;
- @property (nonatomic, strong) MOChatNotificationView *currentNotificationView;
- @property (nonatomic, assign) BOOL isShowingNotification;
- @property (nonatomic, copy) void(^notificationTapHandler)(V2TIMMessage *msg);
- @property (nonatomic, assign) BOOL notificationEnabled;
- @property (nonatomic, strong) dispatch_queue_t serialQueue; // 添加串行队列
- // 在 @interface MOChatNotificationManager () 部分添加
- @property (nonatomic, strong) NSMutableSet<NSNumber *> *currentScenes;
- @end
- @implementation MOChatNotificationManager
- - (void)dealloc {
- // 移除通知观察者
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - 单例方法
- + (instancetype)sharedManager {
- static MOChatNotificationManager *sharedManager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedManager = [[self alloc] init];
- });
- return sharedManager;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- _notificationQueue = [NSMutableArray array];
- _isShowingNotification = NO;
- _notificationEnabled = YES; // 默认启用通知
- _serialQueue = dispatch_queue_create("com.app.notificationManager", DISPATCH_QUEUE_SERIAL);
- _currentScenes = [NSMutableSet set];
-
- // 注册应用生命周期通知
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationDidEnterBackground:)
- name:UIApplicationDidEnterBackgroundNotification
- object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(applicationWillEnterForeground:)
- name:UIApplicationWillEnterForegroundNotification
- object:nil];
- }
- return self;
- }
- #pragma mark - 公共方法
- // 添加场景管理方法
- - (void)enterScene:(MOChatNotificationScene)scene {
- dispatch_async(self.serialQueue, ^{
- [self.currentScenes addObject:@(scene)];
- [self updateNotificationEnabledState];
- });
- }
- - (void)leaveScene:(MOChatNotificationScene)scene {
- dispatch_async(self.serialQueue, ^{
- if ([self.currentScenes containsObject:@(scene)]) {
- [self.currentScenes removeObject:@(scene)];
- }
-
- [self updateNotificationEnabledState];
- });
- }
- - (void)updateNotificationEnabledState {
- BOOL shouldEnable = YES;
-
- // 检查当前是否有不允许显示通知的场景
- for (NSNumber *sceneNumber in self.currentScenes) {
- MOChatNotificationScene scene = [sceneNumber integerValue];
- if (scene >= MOChatNotificationSceneMessageList) { // 大于等于消息列表的场景都不显示通知
- shouldEnable = NO;
- break;
- }
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self setNotificationEnabled:shouldEnable];
- });
- }
- - (void)showNotification:(V2TIMMessage *)notification{
- if (!notification) return;
-
- // 如果通知被禁用,则不显示
- if (!self.notificationEnabled) return;
-
- if(!(notification.elemType == V2TIM_ELEM_TYPE_TEXT ||
- notification.elemType == V2TIM_ELEM_TYPE_IMAGE ||
- notification.elemType == V2TIM_ELEM_TYPE_SOUND ||
- notification.elemType == V2TIM_ELEM_TYPE_VIDEO ||
- notification.elemType == V2TIM_ELEM_TYPE_FILE)) return;
-
- dispatch_async(self.serialQueue, ^{
- // 检查是否有来自同一聊天的消息
- BOOL foundSameChat = NO;
- NSInteger sameIndex = -1;
-
- for (NSInteger i = 0; i < self.notificationQueue.count; i++) {
- V2TIMMessage *existingNotification = self.notificationQueue[i];
- if ([existingNotification.sender isEqualToString:notification.sender]) {
- sameIndex = i;
- foundSameChat = YES;
- break;
- }
- }
-
- if (foundSameChat && sameIndex >= 0 && sameIndex < self.notificationQueue.count) {
- // 直接替换队列中的消息
- [self.notificationQueue replaceObjectAtIndex:sameIndex withObject:notification];
- } else {
- // 添加到队列
-
- if(self.currentNotificationView &&
- [self.currentNotificationView.model.sender isEqualToString:notification.sender]){
- // 如果当前显示的通知来自同一聊天,下面会直接更新它
- }
- else{
- [self.notificationQueue addObject:notification];
- }
- }
-
- // 检查当前显示的通知是否与新通知来自同一聊天,如果是则立即更新
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self.currentNotificationView &&
- [self.currentNotificationView.model.sender isEqualToString:notification.sender]) {
- // 更新当前显示的通知内容
- self.currentNotificationView.model = notification;
- }
- // 如果当前没有显示通知,则开始显示
- else if (!self.isShowingNotification) {
- [self showNextNotification];
- }
- });
- });
- }
- - (void)clearAllNotifications {
- [self.notificationQueue removeAllObjects];
-
- if (self.currentNotificationView) {
- [self.currentNotificationView dismissWithAnimation];
- self.currentNotificationView = nil;
- }
-
- // 额外的安全措施:遍历并移除所有可能残留的通知视图
- [self removeAllNotificationViewsFromWindows];
-
- self.isShowingNotification = NO;
- }
- - (void)setupNotificationTapHandler:(void(^)(V2TIMMessage *msg))handler {
- self.notificationTapHandler = handler;
- }
- - (void)setNotificationEnabled:(BOOL)enabled {
- _notificationEnabled = enabled;
-
- // 如果禁用通知,则清除当前所有通知
- if (!enabled) {
- [self clearAllNotifications];
-
- // 确保所有通知视图都被移除
- [self removeAllNotificationViewsFromWindows];
- }
- }
- - (BOOL)isNotificationEnabled {
- return _notificationEnabled;
- }
- #pragma mark - 私有方法
- - (void)showNextNotification {
-
- // 如果通知被禁用,则不显示
- if (!self.notificationEnabled) return;
-
- if (self.notificationQueue.count == 0) {
- self.isShowingNotification = NO;
- return;
- }
-
- self.isShowingNotification = YES;
-
- // 获取队列中的第一条通知
- V2TIMMessage *notification = [self.notificationQueue firstObject];
- [self.notificationQueue removeObjectAtIndex:0];
-
- // 创建并显示通知视图
- UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
- self.currentNotificationView = [[MOChatNotificationView alloc] initWithModel:notification];
- self.currentNotificationView.delegate = self;
- [keyWindow addSubview:self.currentNotificationView];
- [self.currentNotificationView showWithAnimation];
-
- // 3秒后自动隐藏
- __weak typeof(self) weakSelf = self;
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [weakSelf hideCurrentNotification];
- });
- }
- - (void)hideCurrentNotification {
- if (self.currentNotificationView) {
- __weak typeof(self) weakSelf = self;
- MOChatNotificationView *viewToRemove = self.currentNotificationView;
- self.currentNotificationView = nil;
-
- // 使用动画移除视图
- [viewToRemove dismissWithAnimation];
-
- // 延迟一小段时间后显示下一条通知,避免动画重叠
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 确保视图已被移除
- if ([viewToRemove superview]) {
- [viewToRemove removeFromSuperview];
- }
- [weakSelf showNextNotification];
- });
- }
- }
- #pragma mark - MOChatNotificationViewDelegate
- - (void)didTapNotificationWithChatId:(V2TIMMessage *)msg {
- // 点击通知时,隐藏当前通知并执行回调
- [self hideCurrentNotification];
-
- if (self.notificationTapHandler) {
- self.notificationTapHandler(msg);
- }
- }
- #pragma mark - 辅助方法
- - (void)removeAllNotificationViewsFromWindows {
- // 获取所有窗口
- NSArray *windows = [UIApplication sharedApplication].windows;
- for (UIWindow *window in windows) {
- // 递归查找并移除所有MOChatNotificationView
- [self removeNotificationViewsFromView:window];
- }
- }
- - (void)removeNotificationViewsFromView:(UIView *)view {
- // 检查当前视图是否为MOChatNotificationView
- if ([view isKindOfClass:[MOChatNotificationView class]]) {
- [view removeFromSuperview];
- return; // 如果当前视图已经是MOChatNotificationView,移除后直接返回,不需要继续遍历其子视图
- }
-
- // 创建子视图的副本,因为在遍历过程中可能会修改子视图数组
- NSArray *subviews = [view.subviews copy];
- for (UIView *subview in subviews) {
- [self removeNotificationViewsFromView:subview];
- }
- }
- #pragma mark - 应用生命周期处理
- - (void)applicationDidEnterBackground:(NSNotification *)notification {
- // 应用进入后台时,清理所有通知
- dispatch_async(dispatch_get_main_queue(), ^{
- // 直接移除当前通知视图,不执行动画
- if (self.currentNotificationView) {
- [self.currentNotificationView removeFromSuperview];
- self.currentNotificationView = nil;
- }
-
- // 遍历所有窗口,查找并移除所有MOChatNotificationView
- [self removeAllNotificationViewsFromWindows];
-
- // 清空队列
- [self.notificationQueue removeAllObjects];
- self.isShowingNotification = NO;
- });
- }
- - (void)applicationWillEnterForeground:(NSNotification *)notification {
- // 应用即将进入前台,重置状态
- dispatch_async(dispatch_get_main_queue(), ^{
- // 确保状态正确
- if (self.currentNotificationView) {
- [self.currentNotificationView removeFromSuperview];
- self.currentNotificationView = nil;
- }
-
- // 遍历所有窗口,查找并移除所有MOChatNotificationView
- [self removeAllNotificationViewsFromWindows];
-
- self.isShowingNotification = NO;
-
- // 如果有待显示的通知,则开始显示
- if (self.notificationQueue.count > 0 && self.notificationEnabled) {
- [self showNextNotification];
- }
- });
- }
- @end
|