badge_type to -2 to enable the application's badge number to automatically increase by 1.
Step 2: when the application is started, call the "clearing the application's badge number and notification bar messages" method to clear the local badge number and notification bar messages. The implementation code is as follows.//Sync the badge value to the TPNS server, and the value will be used as the benchmark for the next push- (void)setBadge:(NSInteger)badgeNumber;
setBadge method relies on the Tencent Push Notification Service channel to implement badge value sync. When the application enters the background, the Tencent Push Notification Service channel will be disconnected, and calling the setBadge method will fail. In this case, only the custom badge number scheme is applicable.setBadge method will fail before the connection is resumed. In this case, only the custom badge number scheme is applicable.setBadge must be used in the callback of the registration method.//Obtain the number of notification bar messages[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> *notifications) {NSLog(@"notifications count:%d.",notifications.count);}];
//Set the application's badge number[XGPush defaultManager].xgApplicationBadgeNumber = a + b;
badge_type to the total number of messages (a custom badge number that is equal to or greater than 0). For example, if the total number of messages is 10, set badge_type to 10.//Do not display the number of pushes in the application icon, but retain the push notifications in the system notification bar+ (void)resetBageNumber{if(APNS_IS_IOS11_LATER){//For iOS 11 or later, you only need to set `badgeNumber` to `-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)// Display the badge number in the application icon, but retain the push notifications in the system notification bar.+ (void)resetBageNumber:(int) number{/// If the number is not `0`, set directly.if(number){[XGPush defaultManager].xgApplicationBadgeNumber = number;return;}/// If the number is `0`, set by the following logic:if(APNS_IS_IOS11_LATER){//For iOS 11 or later, you only need to set `badgeNumber` to `-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: push ID of the notification to delete- (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) {//Filter the notification that meets the deletion criteriaObjectID *objectIDFromNotification = [self marshalNotification:notification];if ([objectID isEqual:objectIDFromNotification]) {[identifiersToRemove addObject:notification.request.identifier];}}[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:identifiersToRemove];}];}
// TPNS network connected- (void)xgPushNetworkConnected;// TPNS network disconnected- (void)xgPushNetworkDisconnected;
badge_type to -1 to make the badge number remain unchanged.setApplicationIconBadgeNumber: method of the UIApplication hook system class to print the function call stack to find out the caller information. The code is as follows:
UIApplication+ApplicationIconBadgeNumber.h file#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface UIApplication (ApplicationIconBadgeNumber)@endNS_ASSUME_NONNULL_END
UIApplication+ApplicationIconBadgeNumber.m file#import "UIApplication+ApplicationIconBadgeNumber.h"#import <objc/runtime.h>@implementation UIApplication (ApplicationIconBadgeNumber)// Load class method (called when the code of a certain class is read into memory)+ (void)load{SEL origSel = @selector(setApplicationIconBadgeNumber:);SEL swizSel = @selector(swiz_setApplicationIconBadgeNumber:);[UIApplication swizzleMethods:[self class] originalSelector:origSel swizzledSelector:swizSel];}// Swap the implementations of the two methods.+ (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);}}/// Set the badge using hook.- (void)swiz_setApplicationIconBadgeNumber:(NSInteger)number{NSLog(@" called the `swiz_setApplicationIconBadgeNumber` method.");/// Print the current function call stack.NSArray *syms = [NSThread callStackSymbols];for(int i = 0 ; i < [syms count]; i++){NSLog(@"<%@ %p> %@ - caller: %@ ", [self class], self, NSStringFromSelector(_cmd),[syms objectAtIndex:i]);}// Jump to the `setApplicationIconBadgeNumber` method when this statement is executed.[self swiz_setApplicationIconBadgeNumber:number];}@end

Feedback