|
|
@@ -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{
|