//将角标值同步到移动推送服务器,下次推送时以此值为基准- (void)setBadge:(NSInteger)badgeNumber;
//需要获取通知栏的消息数[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *notifications) {NSLog(@"notifications count:%d.",notifications.count);}];
//设置应用角标数[XGPush defaultManager].xgApplicationBadgeNumber = a + b;
//不在appIcon上显示推送数量,但是在系统通知栏保留推送通知的方法+ (void)resetBageNumber{if(APNS_IS_IOS11_LATER){//iOS 11后,直接设置badgeNumber = -1就生效了[UIApplication sharedApplication].applicationIconBadgeNumber = -1;}else{UILocalNotification *clearEpisodeNotification = [[UILocalNotification alloc] init];clearEpisodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(0.3)];clearEpisodeNotification.timeZone = [NSTimeZone defaultTimeZone];clearEpisodeNotification.applicationIconBadgeNumber = -1;[[UIApplication sharedApplication] scheduleLocalNotification:clearEpisodeNotification];}}
#define APNS_IS_IOS11_LATER ([UIDevice currentDevice].systemVersion.floatValue >= 11.0f)//在appIcon上推送角标数量逻辑,但是在系统通知栏保留推送通知的方法+ (void)resetBageNumber:(int) number{/// 如果是非0数,直接设置if(number){[XGPush defaultManager].xgApplicationBadgeNumber = number;return;}/// 如果是0,则通过如下逻辑设置if(APNS_IS_IOS11_LATER){//iOS 11后,直接设置badgeNumber = -1就生效了[UIApplication sharedApplication].applicationIconBadgeNumber = -1;}else{UILocalNotification *clearEpisodeNotification = [[UILocalNotificationalloc] init];clearEpisodeNotification.fireDate = [NSDatedateWithTimeIntervalSinceNow:(0.3)];clearEpisodeNotification.timeZone = [NSTimeZonedefaultTimeZone];clearEpisodeNotification.applicationIconBadgeNumber = -1;[[UIApplication sharedApplication] scheduleLocalNotification:clearEpisodeNotification];}}
//objectID:通知下发的消息id,用以辨识通知- (void)removeNotificationsForObjectID:(ObjectID *)objectID {__weak __typeof(self) weakSelf = self;[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *notifications) {__strong __typeof(weakSelf) self = weakSelf;NSMutableArray <NSString *> *identifiersToRemove = [@[] mutableCopy];for (UNNotification *notification in notifications) {//筛选符合删除条件的通知ObjectID *objectIDFromNotification = [self marshalNotification:notification];if ([objectID isEqual:objectIDFromNotification]) {[identifiersToRemove addObject:notification.request.identifier];}}[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:identifiersToRemove];}];}
// TPNS网络连接成功- (void)xgPushNetworkConnected;// TPNS网络连接断开- (void)xgPushNetworkDisconnected;
#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface UIApplication (ApplicationIconBadgeNumber)@endNS_ASSUME_NONNULL_END
#import "UIApplication+ApplicationIconBadgeNumber.h"#import <objc/runtime.h>@implementation UIApplication (ApplicationIconBadgeNumber)//load类方法(当某个类的代码被读到内存后调用)+ (void)load{SEL origSel = @selector(setApplicationIconBadgeNumber:);SEL swizSel = @selector(swiz_setApplicationIconBadgeNumber:);[UIApplication swizzleMethods:[self class] originalSelector:origSel swizzledSelector:swizSel];}//交换两个方法的实现+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel{Method origMethod = class_getInstanceMethod(class, origSel);Method swizMethod = class_getInstanceMethod(class, swizSel);BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));if (didAddMethod){class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));}else{method_exchangeImplementations(origMethod, swizMethod);}}/// hook设置角标- (void)swiz_setApplicationIconBadgeNumber:(NSInteger)number{NSLog(@"调用了swiz_setApplicationIconBadgeNumber方法");/// 打印当前函数调用堆栈NSArray *syms = [NSThread callStackSymbols];for(int i = 0 ; i < [syms count]; i++){NSLog(@"<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:i]);}//执行这句的时候跳转到setApplicationIconBadgeNumber方法中[self swiz_setApplicationIconBadgeNumber:number];}@end
本页内容是否解决了您的问题?