Skip to content

Commit 2570a4d

Browse files
authored
Merge pull request #1335 from OneSignal/rebase_fix_badge_clearing
Fix badge clearing when calling clearAll
2 parents c2d4acf + 3da18b0 commit 2570a4d

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ NS_SWIFT_NAME(onClick(event:));
114114

115115
+ (void)handleWillShowInForegroundForNotification:(OSNotification *_Nonnull)notification completion:(OSNotificationDisplayResponse _Nonnull)completion;
116116
+ (void)handleNotificationActionWithUrl:(NSString* _Nullable)url actionID:(NSString* _Nonnull)actionID;
117-
+ (BOOL)clearBadgeCount:(BOOL)fromNotifOpened;
117+
+ (BOOL)clearBadgeCount:(BOOL)fromNotifOpened fromClearAll:(BOOL)fromClearAll;
118118

119119
+ (BOOL)receiveRemoteNotification:(UIApplication* _Nonnull)application UserInfo:(NSDictionary* _Nonnull)userInfo completionHandler:(void (^_Nonnull)(UIBackgroundFetchResult))completionHandler;
120120
+ (void)notificationReceived:(NSDictionary* _Nonnull)messageDict wasOpened:(BOOL)opened;

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ + (void)presentAppSettings {
383383

384384
+ (void)clearAll {
385385
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
386-
// TODO: Determine if we also need to call clearBadgeCount
387-
[self clearBadgeCount:false];
386+
// removing delivered notifications doesn't update the badge count
387+
[self clearBadgeCount:false fromClearAll:true];
388388
}
389389

390390
+ (BOOL)registerForAPNsToken {
@@ -686,7 +686,7 @@ + (void)handleNotificationOpened:(NSDictionary*)messageDict
686686
[self launchWebURL:notification.launchURL]; //TODO: where should this live?
687687
}
688688

689-
[self clearBadgeCount:true];
689+
[self clearBadgeCount:true fromClearAll:false];
690690

691691
NSString* actionID = NULL;
692692
if (actionType == OSNotificationActionTypeActionTaken) {
@@ -755,7 +755,7 @@ + (void)displayWebView:(NSURL*)url {
755755
openUrlBlock(true);
756756
}
757757

758-
+ (BOOL)clearBadgeCount:(BOOL)fromNotifOpened {
758+
+ (BOOL)clearBadgeCount:(BOOL)fromNotifOpened fromClearAll:(BOOL)fromClearAll {
759759

760760
NSNumber *disableBadgeNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:ONESIGNAL_DISABLE_BADGE_CLEARING];
761761

@@ -764,8 +764,11 @@ + (BOOL)clearBadgeCount:(BOOL)fromNotifOpened {
764764
else
765765
_disableBadgeClearing = NO;
766766

767-
if (_disableBadgeClearing)
767+
if (_disableBadgeClearing && !fromClearAll) {
768+
// The customer could have manually changed the badge value. We must ensure our cached value will match the current state.
769+
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:[UIApplication sharedApplication].applicationIconBadgeNumber];
768770
return false;
771+
}
769772

770773
bool wasBadgeSet = [UIApplication sharedApplication].applicationIconBadgeNumber > 0;
771774

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ + (void)init {
506506
}];
507507
*/
508508

509-
[OSNotificationsManager clearBadgeCount:false];
509+
[OSNotificationsManager clearBadgeCount:false fromClearAll:false];
510510
[self startOutcomes];
511511
[self startLocation];
512512
[self startTrackIAP];
@@ -798,7 +798,6 @@ + (void)load {
798798
*/
799799
- (void)onesignalSetApplicationIconBadgeNumber:(NSInteger)badge {
800800
[OneSignalExtensionBadgeHandler updateCachedBadgeValue:badge];
801-
802801
[self onesignalSetApplicationIconBadgeNumber:badge];
803802
}
804803

iOS_SDK/OneSignalSDK/Source/OneSignalTracker.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ + (void)applicationForegrounded {
114114
// [OneSignal receivedInAppMessageJson:nil];
115115
}
116116

117-
[OSNotificationsManager clearBadgeCount:false];
117+
[OSNotificationsManager clearBadgeCount:false fromClearAll:false];
118118
}
119119

120120
+ (void)applicationBackgrounded {

0 commit comments

Comments
 (0)