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
Keep a previous badge count as well as current
* When a notification foreground display is cancelled, the badge count in that notification (if any) does not apply. We have to revert our cached badge count back to the previous. We store the previous as we cannot use a getter for badge count anymore.
  • Loading branch information
nan-li committed May 28, 2025
commit 1fe4fc1a7eb1180d75223c6d8fb09b0a6523a4fa
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
#import <Foundation/Foundation.h>

@interface OneSignalBadgeHelpers : NSObject
+ (void)updateCachedBadgeValue:(NSInteger)value;
+ (void)updateCachedBadgeValue:(NSInteger)value usePreviousBadgeCount:(BOOL)usePrevious;
@end
18 changes: 16 additions & 2 deletions iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalBadgeHelpers.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,23 @@
#import "OneSignalCommonDefines.h"

@implementation OneSignalBadgeHelpers
+ (void)updateCachedBadgeValue:(NSInteger)value {

/**
Store a previous badge count so that we can revert to this cached value when a notification display is cancelled.
When `usePreviousBadgeCount` is `true`, the `value` passed to this method will be unused.
*/
+ (void)updateCachedBadgeValue:(NSInteger)value usePreviousBadgeCount:(BOOL)usePrevious {
// Since badge logic can be executed in an extension, we need to use app groups to get
// a shared NSUserDefaults from the app group suite name
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:value];
if (usePrevious) {
// Keep the PREVIOUS_ONESIGNAL_BADGE_KEY value unchanged
NSInteger previousBadgeCount = [OneSignalUserDefaults.initShared getSavedIntegerForKey:PREVIOUS_ONESIGNAL_BADGE_KEY defaultValue:0];
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:previousBadgeCount];
} else {
NSInteger previousBadgeCount = [OneSignalUserDefaults.initShared getSavedIntegerForKey:ONESIGNAL_BADGE_KEY defaultValue:0];
[OneSignalUserDefaults.initShared saveIntegerForKey:PREVIOUS_ONESIGNAL_BADGE_KEY withValue:previousBadgeCount];
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:value];
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
#define ONESIGNAL_DISABLE_BADGE_CLEARING @"OneSignal_disable_badge_clearing"
#define ONESIGNAL_APP_GROUP_NAME_KEY @"OneSignal_app_groups_key"
#define ONESIGNAL_BADGE_KEY @"onesignalBadgeCount"
/// Store the previous badge count to read for a cancelled notification display event
#define PREVIOUS_ONESIGNAL_BADGE_KEY @"previousOnesignalBadgeCount"

// Firebase
#define ONESIGNAL_FB_ENABLE_FIREBASE @"OS_ENABLE_FIREBASE_ANALYTICS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ + (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request
//make sure the OneSignal cached value is updated to this value
if (!notification.badgeIncrement) {
if (notification.hasBadge)
[OneSignalBadgeHelpers updateCachedBadgeValue:notification.badge];
[OneSignalBadgeHelpers updateCachedBadgeValue:notification.badge usePreviousBadgeCount:false];

return;
}
Expand All @@ -50,7 +50,7 @@ + (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request

replacementContent.badge = @(currentValue);

[OneSignalBadgeHelpers updateCachedBadgeValue:currentValue];
[OneSignalBadgeHelpers updateCachedBadgeValue:currentValue usePreviousBadgeCount:false];
}

+ (NSInteger)currentCachedBadgeValue {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ - (void)onesignalGetNotificationSettingsWithCompletionHandler:(void(^)(UNNotific
necessary as there is no equivalent "getBadgeCount" method available.
*/
- (void)onesignalSetBadgeCount:(NSInteger)badge withCompletionHandler:(void(^)(NSError *error))completionHandler {
[OneSignalBadgeHelpers updateCachedBadgeValue:badge];
[OneSignalBadgeHelpers updateCachedBadgeValue:badge usePreviousBadgeCount:false];
[self onesignalSetBadgeCount:badge withCompletionHandler:completionHandler];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ - (void)complete:(OSDisplayableNotification *)notification {
reset the badge count to the value prior to receipt of this notif
*/
if (!notification) {
NSInteger previousBadgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
[OneSignalBadgeHelpers updateCachedBadgeValue:previousBadgeCount];
// The badge count value of -99 will not be used, the previous badge count will be set
[OneSignalBadgeHelpers updateCachedBadgeValue:-99 usePreviousBadgeCount:true];
}
if (_completion) {
_completion(notification);
Expand Down
2 changes: 1 addition & 1 deletion iOS_SDK/OneSignalSDK/Source/OneSignal.m
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ + (void)load {
We swizzle the 'setApplicationIconBadgeNumber()' to intercept these calls so we always know the latest count
*/
- (void)onesignalSetApplicationIconBadgeNumber:(NSInteger)badge {
[OneSignalBadgeHelpers updateCachedBadgeValue:badge];
[OneSignalBadgeHelpers updateCachedBadgeValue:badge usePreviousBadgeCount:false];
[self onesignalSetApplicationIconBadgeNumber:badge];
}

Expand Down
Loading