Bläddra i källkod

加好友申请通知、消息未读数展示

zwp 5 månader sedan
förälder
incheckning
3d3caa4d6d

+ 4 - 0
AIIM.xcodeproj/project.pbxproj

@@ -275,6 +275,8 @@
 				INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
 				INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen.storyboard;
 				INFOPLIST_KEY_UIMainStoryboardFile = Main;
+				INFOPLIST_KEY_UIStatusBarHidden = YES;
+				INFOPLIST_KEY_UIStatusBarStyle = UIStatusBarStyleLightContent;
 				INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
 				INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
 				IPHONEOS_DEPLOYMENT_TARGET = 15.6;
@@ -331,6 +333,8 @@
 				INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
 				INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen.storyboard;
 				INFOPLIST_KEY_UIMainStoryboardFile = Main;
+				INFOPLIST_KEY_UIStatusBarHidden = YES;
+				INFOPLIST_KEY_UIStatusBarStyle = UIStatusBarStyleLightContent;
 				INFOPLIST_KEY_UISupportedInterfaceOrientations = UIInterfaceOrientationPortrait;
 				INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown";
 				IPHONEOS_DEPLOYMENT_TARGET = 15.6;

+ 3 - 0
AIIM/AppDelegate.h

@@ -42,5 +42,8 @@ static AppDelegate * _Nullable app = nil;
 
 +(void)changeFloadViewState:(BOOL)state;
 
+- (void)resetAddFriendBadgeCount;
+- (void)updateBadgeCount:(NSInteger)badgeCount;
+
 @end
 

+ 77 - 8
AIIM/AppDelegate.m

@@ -55,6 +55,9 @@
 
 @property (nonatomic) BOOL isNotification;
 
+@property (nonatomic, assign) NSInteger badgeCount;
+@property (nonatomic, assign) NSInteger addFriendBadgeCount;
+
 @end
 
 @implementation AppDelegate
@@ -355,17 +358,75 @@
 }
 
 - (void)addFriend{
-    NSLog(@"addFriend------------");
-    UITabBarController * tabbarVc = (UITabBarController *)self.window.rootViewController;
-    UITabBarItem *tabBarItem = tabbarVc.tabBar.items[1];
-    tabBarItem.badgeValue = @"";
+    self.addFriendBadgeCount++;
+    NSLog(@"addFriend------------:%ld", self.addFriendBadgeCount);
+    [self updateTabBarBadgeValueAtIndex:1 badgeCount:self.addFriendBadgeCount];
+
+    [self sendAddFreindNotice];
+    [self updateApplicationIconBadgeNumber];
+}
+
+- (void)sendAddFreindNotice {
+    // 发送本地通知
+    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
+    content.title = @"好友申请";
+    content.body = @"您收到了新的好友申请";
+    content.sound = [UNNotificationSound defaultSound];
+    content.badge = @(self.addFriendBadgeCount);
+    
+    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1 repeats:NO];
+    
+    NSString *identifier = [NSString stringWithFormat:@"FriendRequest_%@", [[NSUUID UUID] UUIDString]];
+    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
+    
+    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
+        if (error) {
+            NSLog(@"发送本地通知失败: %@", error);
+        } else {
+            NSLog(@"发送本地通知成功");
+        }
+    }];
 }
 
-- (void)exGroup{
-    NSLog(@"exGroup------------");
+- (void)updateApplicationIconBadgeNumber {
+    NSInteger total = self.badgeCount + self.addFriendBadgeCount;
+    if (@available(iOS 16.0, *)) {
+        [[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:total withCompletionHandler:^(NSError * _Nullable error) {
+            if (error) {
+                NSLog(@"设置消息未读数 error: %@", error);
+            }
+        }];
+    } else {
+        // Fallback on earlier versions
+        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:total];
+    }
+}
+
+- (void)resetAddFriendBadgeCount {
+    self.addFriendBadgeCount = 0;
+    [self updateTabBarBadgeValueAtIndex:1 badgeCount:0];
+    [self updateApplicationIconBadgeNumber];
+}
+
+- (void)resetBadgeCount {
+    self.badgeCount = 0;
+    [self updateApplicationIconBadgeNumber];
+}
+
+- (void)updateTabBarBadgeValueAtIndex:(NSInteger)index badgeCount:(NSInteger)badgeCount {
     UITabBarController * tabbarVc = (UITabBarController *)self.window.rootViewController;
-    UITabBarItem *tabBarItem = tabbarVc.tabBar.items[2];
-    tabBarItem.badgeValue = @"";
+    if(index < 0 || index >= tabbarVc.tabBar.items.count){
+        return;
+    }
+    UITabBarItem *tabBarItem = tabbarVc.tabBar.items[index];
+    NSString *badgeValue = badgeCount > 0 ? [NSString stringWithFormat:@"%ld", badgeCount] : nil;
+    badgeValue = badgeValue.length > 2 ? @"99+" : badgeValue;
+    tabBarItem.badgeValue = badgeValue;
+}
+
+- (void)updateBadgeCount:(NSInteger)badgeCount {
+    self.badgeCount = badgeCount;
+    [self updateApplicationIconBadgeNumber];
 }
 
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
@@ -662,6 +723,12 @@
 }
 
 #pragma mark 用户点击消息推送或滑动
+- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
+    completionHandler(UNNotificationPresentationOptionList | UNNotificationPresentationOptionBanner |
+                          UNNotificationPresentationOptionSound |
+                          UNNotificationPresentationOptionBadge);
+}
+
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
     
     // 处理用户对通知的响应(点击、滑动等)
@@ -770,6 +837,8 @@
     [self clearVoipToken];
     [self openLoginController];
     [self endWebsocket];
+    [self resetBadgeCount];
+    [self resetAddFriendBadgeCount];
 }
 
 -(void)clearVoipToken{

+ 13 - 1
AIIM/Common/Store/ChatListStore.m

@@ -13,7 +13,7 @@
 #import "GroupNetApi.h"
 #import <Bugly/Bugly.h>
 #import "CryptoAES.h"
-
+#import "AppDelegate.h"
 @interface ChatListStore()
 @property(nonatomic, strong) NSArray *toparray;
 @property(nonatomic, strong) NSArray *nmarray;
@@ -79,6 +79,18 @@
             if(self.delegate){
                 [self.delegate ChatListChange:reschats.copy];
             }
+
+            NSInteger unreadCount = 0;
+            for (NSDictionary *item in reschats) {
+                NSString *unreadCountStr = item[@"unreadCount"];
+                if(unreadCountStr.intValue>0){
+                    unreadCount = unreadCount + unreadCountStr.intValue;
+                }
+            }
+            dispatch_main_async_safe(^{
+                AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
+                [app updateBadgeCount:unreadCount];
+            })
         }
         else{
             self.nmarray =@[];

+ 5 - 0
AIIM/Controller/chat/ChatIndexController.m

@@ -21,6 +21,11 @@
 @end
 
 @implementation ChatIndexController
+
+- (UIStatusBarStyle)preferredStatusBarStyle {
+    return UIStatusBarStyleLightContent;
+}
+
 -(void)viewDidLoad{
     [super viewDidLoad];
     NSLog(@"ChatIndexController viewDidLoad");

+ 1 - 1
AIIM/Controller/chat/ChatListCell.m

@@ -51,7 +51,7 @@
     NSString *count = data[@"unreadCount"];
     NSLog(@"count:%@",count);
     if(count.intValue>99){
-        _countText.text=@"99";
+        _countText.text=@"99+";
         _countText.alpha = 1;
     }
     else if(count.intValue==0){

+ 9 - 5
AIIM/Controller/friend/FriendListController.m

@@ -13,7 +13,7 @@
 #import "FriendController.h"
 #import "EXFriendController.h"
 #import "addFriendController.h"
-
+#import "AppDelegate.h"
 @interface FriendListController()<UITableViewDelegate,UITableViewDataSource>
 
 
@@ -28,6 +28,10 @@
 
 @implementation FriendListController
 
+- (UIStatusBarStyle)preferredStatusBarStyle {
+    return UIStatusBarStyleLightContent;
+}
+
 -(void)viewDidLoad{
     [super viewDidLoad];
     
@@ -57,10 +61,6 @@
     [self getFriendlist];
     [self getexfriends];
     [self startTimer];
-    // 获取TabBarController
-    UITabBarController *tabBarController = (UITabBarController *)self.parentViewController;
-    // 移除红点(使用系统方法)
-    tabBarController.tabBar.items[1].badgeValue = nil;
 }
 -(void)viewWillDisappear:(BOOL)animated{
     [super viewWillDisappear:animated];
@@ -142,6 +142,10 @@
     UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:friendctr];
     uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
     [self presentViewController :uiNavC animated:YES completion:nil];
+    
+    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
+    [app resetAddFriendBadgeCount];
+    
 }
 
 

+ 0 - 4
AIIM/Controller/group/GroupListController.m

@@ -30,10 +30,6 @@
 -(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:YES];
     [self getGrouplistData];
-    // 获取TabBarController
-    UITabBarController *tabBarController = (UITabBarController *)self.parentViewController;
-    // 移除红点(使用系统方法)
-    tabBarController.tabBar.items[2].badgeValue = nil;
 }
 
 -(void)initSubView{