Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use new setBadgeCount API on iOS 16+
* The method `applicationIconBadgeNumber` is deprecated in iOS 17 - see https://developer.apple.com/documentation/uikit/uiapplication/applicationiconbadgenumber
* Its replacement is `setBadgeCount:withCompletionHandler:` but there is no equivalent getter.
* Use this new API on ios 16+ and swizzle it just like we do for applicationIconBadgeNumber
  • Loading branch information
nan-li committed May 28, 2025
commit 6d9b74c16f9f6c7247ba8dfdc28db73550515ba7
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ + (void)swizzleSelectors {
[OneSignalNotificationsUNUserNotificationCenter class],
@selector(onesignalGetNotificationSettingsWithCompletionHandler:)
);
injectSelector(
[UNUserNotificationCenter class],
@selector(setBadgeCount:withCompletionHandler:),
[OneSignalNotificationsUNUserNotificationCenter class],
@selector(onesignalSetBadgeCount:withCompletionHandler:)
);
}

+ (void)registerDelegate {
Expand Down Expand Up @@ -167,6 +173,15 @@ - (void)onesignalGetNotificationSettingsWithCompletionHandler:(void(^)(UNNotific
[self onesignalGetNotificationSettingsWithCompletionHandler:wrapperBlock];
}

/**
In order for the badge count to be consistent even in situations where the developer manually sets the badge number,
we swizzle the 'setBadgeCount()' method for ios 16+ to intercept these calls so we always know the latest count. This is especially
necessary as there is no equivalent "getBadgeCount" method available.
*/
- (void)onesignalSetBadgeCount:(NSInteger)badge withCompletionHandler:(void(^)(NSError *error))completionHandler {
[OneSignalBadgeHelpers updateCachedBadgeValue:badge];
[self onesignalSetBadgeCount:badge withCompletionHandler:completionHandler];
}

// A Set to keep track of which classes we have already swizzled so we only
// swizzle each one once. If we swizzled more than once then this will create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,17 @@ + (void)clearBadgeCount:(BOOL)fromNotifOpened fromClearAll:(BOOL)fromClearAll {
bool wasBadgeSet = [UIApplication sharedApplication].applicationIconBadgeNumber > 0;

if (fromNotifOpened || wasBadgeSet) {
[OneSignalCoreHelper runOnMainThread:^{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}];
if (@available(iOS 16.0, *)) {
[[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:0 withCompletionHandler:^(NSError * _Nullable error) {
if (error) {
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"clearBadgeCount encountered error setting badge count: %@", error]];
}
}];
} else {
[OneSignalCoreHelper runOnMainThread:^{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}];
}
}
}

Expand Down