Skip to content

Commit

Permalink
fix(ios): use new icon badge APIs / correctly clear badge
Browse files Browse the repository at this point in the history
on iOS16+ etc there are new badging APIs and to correctly set icon
to zero on these devices you cannot use the old-api + set to -1 trick

it apparently still leaves the notification center notifications intact
though, which is nice
  • Loading branch information
mikehardy committed Oct 2, 2024
1 parent 53692a9 commit 2c543f8
Showing 1 changed file with 52 additions and 10 deletions.
62 changes: 52 additions & 10 deletions ios/NotifeeCore/NotifeeCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,24 @@ + (void)getInitialNotification:(notifeeMethodNSDictionaryBlock)block {

+ (void)setBadgeCount:(NSInteger)count withBlock:(notifeeMethodVoidBlock)block {
if (![NotifeeCoreUtil isAppExtension]) {
// If count is 0, set to -1 instead to avoid notifications in tray being cleared
NSInteger newCount = count == 0 ? -1 : count;
UIApplication *application = (UIApplication *)[NotifeeCoreUtil notifeeUIApplication];
[application setApplicationIconBadgeNumber:newCount];
if (@available(iOS 16.0, macOS 10.13, macCatalyst 16.0, tvOS 16.0, visionOS 1.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setBadgeCount:count
withCompletionHandler:^(NSError *error) {
if (error) {
NSLog(@"NotifeeCore: Could not setBadgeCount: %@", error);
block(error);
} else {
block(nil);
}
}];
return;
} else {
// If count is 0, set to -1 instead to avoid notifications in tray being cleared
NSInteger newCount = count == 0 ? 0 : count; //-1 : count;
UIApplication *application = (UIApplication *)[NotifeeCoreUtil notifeeUIApplication];
[application setApplicationIconBadgeNumber:newCount];
}
}
block(nil);
}
Expand All @@ -763,21 +777,49 @@ + (void)incrementBadgeCount:(NSInteger)incrementBy withBlock:(notifeeMethodVoidB

NSInteger newCount = currentCount + incrementBy;

[application setApplicationIconBadgeNumber:newCount];
block(nil);
if (@available(iOS 16.0, macOS 10.13, macCatalyst 16.0, tvOS 16.0, visionOS 1.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setBadgeCount:newCount
withCompletionHandler:^(NSError *error) {
if (error) {
NSLog(@"NotifeeCore: Could not incrementBadgeCount: %@", error);
block(error);
} else {
block(nil);
}
}];
return;
} else {
[application setApplicationIconBadgeNumber:newCount];
}
}
block(nil);
}

+ (void)decrementBadgeCount:(NSInteger)decrementBy withBlock:(notifeeMethodVoidBlock)block {
if (![NotifeeCoreUtil isAppExtension]) {
UIApplication *application = (UIApplication *)[NotifeeCoreUtil notifeeUIApplication];
NSInteger currentCount = application.applicationIconBadgeNumber;
NSInteger newCount = currentCount - decrementBy;
// If count is 0 or less, set to -1 instead to avoid notifications in tray being cleared
if (newCount < 1) {
newCount = -1;
if (@available(iOS 16.0, macOS 10.13, macCatalyst 16.0, tvOS 16.0, visionOS 1.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setBadgeCount:newCount
withCompletionHandler:^(NSError *error) {
if (error) {
NSLog(@"NotifeeCore: Could not incrementBadgeCount: %@", error);
block(error);
} else {
block(nil);
}
}];
return;
} else {
// If count is 0 or less, set to -1 instead to avoid notifications in tray being cleared
if (newCount < 1) {
newCount = -1;
}
[application setApplicationIconBadgeNumber:newCount];
}
[application setApplicationIconBadgeNumber:newCount];
}

block(nil);
Expand Down

0 comments on commit 2c543f8

Please sign in to comment.