Skip to content

Commit 6d9b74c

Browse files
committed
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
1 parent 5114281 commit 6d9b74c

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

iOS_SDK/OneSignalSDK/OneSignalNotifications/Categories/UNUserNotificationCenter+OneSignalNotifications.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ + (void)swizzleSelectors {
9797
[OneSignalNotificationsUNUserNotificationCenter class],
9898
@selector(onesignalGetNotificationSettingsWithCompletionHandler:)
9999
);
100+
injectSelector(
101+
[UNUserNotificationCenter class],
102+
@selector(setBadgeCount:withCompletionHandler:),
103+
[OneSignalNotificationsUNUserNotificationCenter class],
104+
@selector(onesignalSetBadgeCount:withCompletionHandler:)
105+
);
100106
}
101107

102108
+ (void)registerDelegate {
@@ -167,6 +173,15 @@ - (void)onesignalGetNotificationSettingsWithCompletionHandler:(void(^)(UNNotific
167173
[self onesignalGetNotificationSettingsWithCompletionHandler:wrapperBlock];
168174
}
169175

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

171186
// A Set to keep track of which classes we have already swizzled so we only
172187
// swizzle each one once. If we swizzled more than once then this will create

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,17 @@ + (void)clearBadgeCount:(BOOL)fromNotifOpened fromClearAll:(BOOL)fromClearAll {
802802
bool wasBadgeSet = [UIApplication sharedApplication].applicationIconBadgeNumber > 0;
803803

804804
if (fromNotifOpened || wasBadgeSet) {
805-
[OneSignalCoreHelper runOnMainThread:^{
806-
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
807-
}];
805+
if (@available(iOS 16.0, *)) {
806+
[[UNUserNotificationCenter currentNotificationCenter] setBadgeCount:0 withCompletionHandler:^(NSError * _Nullable error) {
807+
if (error) {
808+
[OneSignalLog onesignalLog:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"clearBadgeCount encountered error setting badge count: %@", error]];
809+
}
810+
}];
811+
} else {
812+
[OneSignalCoreHelper runOnMainThread:^{
813+
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
814+
}];
815+
}
808816
}
809817
}
810818

0 commit comments

Comments
 (0)