File tree Expand file tree Collapse file tree 2 files changed +26
-3
lines changed
iOS_SDK/OneSignalSDK/OneSignalNotifications Expand file tree Collapse file tree 2 files changed +26
-3
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments