Skip to content

Commit 208ff4e

Browse files
committed
Centralize method to cache badge count
* Use one method to cache badge count and move this method `updateCachedBadgeValue` out of OneSignalExtension so it can be used by other modules. Move it to a badge helpers class in OneSignalCore.
1 parent 7fe3148 commit 208ff4e

File tree

9 files changed

+84
-12
lines changed

9 files changed

+84
-12
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
3C277D7E2BD76E0000857606 /* OSIdentityModelRepo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */; };
7474
3C2C7DC8288F3C020020F9AE /* OSSubscriptionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */; };
7575
3C2D8A5928B4C4E300BE41F6 /* OSDelta.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */; };
76+
3C2DB2F12DE6CB5E0006B905 /* OneSignalBadgeHelpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C2DB2EF2DE6CB5E0006B905 /* OneSignalBadgeHelpers.h */; settings = {ATTRIBUTES = (Public, ); }; };
77+
3C2DB2F22DE6CB5E0006B905 /* OneSignalBadgeHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C2DB2F02DE6CB5E0006B905 /* OneSignalBadgeHelpers.m */; };
7678
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
7779
3C44673F296D09CC0039A49E /* OneSignalMobileProvision.h in Headers */ = {isa = PBXBuildFile; fileRef = 912411FC1E73342200E41FD7 /* OneSignalMobileProvision.h */; settings = {ATTRIBUTES = (Public, ); }; };
7880
3C448B9D2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C448B9B2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.h */; };
@@ -1251,6 +1253,8 @@
12511253
3C2C7DC2288E007E0020F9AE /* UnitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UnitTests-Bridging-Header.h"; sourceTree = "<group>"; };
12521254
3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSSubscriptionModel.swift; sourceTree = "<group>"; };
12531255
3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSDelta.swift; sourceTree = "<group>"; };
1256+
3C2DB2EF2DE6CB5E0006B905 /* OneSignalBadgeHelpers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalBadgeHelpers.h; sourceTree = "<group>"; };
1257+
3C2DB2F02DE6CB5E0006B905 /* OneSignalBadgeHelpers.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalBadgeHelpers.m; sourceTree = "<group>"; };
12541258
3C448B9B2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSBackgroundTaskHandlerImpl.h; sourceTree = "<group>"; };
12551259
3C448B9C2936ADFD002F96BC /* OSBackgroundTaskHandlerImpl.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSBackgroundTaskHandlerImpl.m; sourceTree = "<group>"; };
12561260
3C448BA12936B474002F96BC /* OSBackgroundTaskManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSBackgroundTaskManager.swift; sourceTree = "<group>"; };
@@ -2596,6 +2600,8 @@
25962600
DE7D182C270273B0002D3A5D /* OSNotification.h */,
25972601
454F94F41FAD2E5A00D74CCF /* OSNotification.m */,
25982602
454F94F61FAD2EC300D74CCF /* OSNotification+Internal.h */,
2603+
3C2DB2EF2DE6CB5E0006B905 /* OneSignalBadgeHelpers.h */,
2604+
3C2DB2F02DE6CB5E0006B905 /* OneSignalBadgeHelpers.m */,
25992605
3CE8CC4C2911ADD1000DB0D3 /* OSDeviceUtils.h */,
26002606
3C47A972292642B100312125 /* OneSignalConfigManager.h */,
26012607
3C47A973292642B100312125 /* OneSignalConfigManager.m */,
@@ -3138,6 +3144,7 @@
31383144
DE7D182F270275FF002D3A5D /* OneSignalTrackFirebaseAnalytics.h in Headers */,
31393145
DEF7845C2912E89200A1F3A5 /* OSObservable.h in Headers */,
31403146
DE7D1875270375FF002D3A5D /* OSReattemptRequest.h in Headers */,
3147+
3C2DB2F12DE6CB5E0006B905 /* OneSignalBadgeHelpers.h in Headers */,
31413148
DEF784652912FB2200A1F3A5 /* OSDialogInstanceManager.h in Headers */,
31423149
DEF78493291479B200A1F3A5 /* OneSignalSelectorHelpers.h in Headers */,
31433150
DE7D1862270374EE002D3A5D /* OSJSONHandling.h in Headers */,
@@ -4427,6 +4434,7 @@
44274434
DEF784642912FA5100A1F3A5 /* OSDialogInstanceManager.m in Sources */,
44284435
DE7D183B27027EFC002D3A5D /* NSURL+OneSignal.m in Sources */,
44294436
DE7D186B270374EE002D3A5D /* OneSignalRequest.m in Sources */,
4437+
3C2DB2F22DE6CB5E0006B905 /* OneSignalBadgeHelpers.m in Sources */,
44304438
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */,
44314439
3CCF44BF299B17290021964D /* OneSignalWrapper.m in Sources */,
44324440
DEF78492291479B200A1F3A5 /* OneSignalSelectorHelpers.m in Sources */,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2025 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
30+
@interface OneSignalBadgeHelpers : NSObject
31+
+ (void)updateCachedBadgeValue:(NSInteger)value;
32+
@end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2025 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "OneSignalBadgeHelpers.h"
29+
#import "OneSignalUserDefaults.h"
30+
#import "OneSignalCommonDefines.h"
31+
32+
@implementation OneSignalBadgeHelpers
33+
+ (void)updateCachedBadgeValue:(NSInteger)value {
34+
// Since badge logic can be executed in an extension, we need to use app groups to get
35+
// a shared NSUserDefaults from the app group suite name
36+
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:value];
37+
}
38+
@end

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalCore.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#import <OneSignalCore/OSLocation.h>
5959
#import <OneSignalCore/OSBundleUtils.h>
6060
#import <OneSignalCore/OneSignalClientError.h>
61+
#import <OneSignalCore/OneSignalBadgeHelpers.h>
6162

6263
// TODO: Testing: Should this class be defined in this file?
6364
@interface OneSignalCoreImpl : NSObject

iOS_SDK/OneSignalSDK/OneSignalExtension/OneSignalExtensionBadgeHandler.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@
3131

3232
@interface OneSignalExtensionBadgeHandler : NSObject
3333
+ (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request withNotification:(OSNotification *)notification withMutableNotificationContent:(UNMutableNotificationContent *)replacementContent;
34-
+ (void)updateCachedBadgeValue:(NSInteger)value;
3534
+ (NSInteger)currentCachedBadgeValue;
3635
@end

iOS_SDK/OneSignalSDK/OneSignalExtension/OneSignalExtensionBadgeHandler.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ + (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request
3535
//make sure the OneSignal cached value is updated to this value
3636
if (!notification.badgeIncrement) {
3737
if (notification.hasBadge)
38-
[OneSignalExtensionBadgeHandler updateCachedBadgeValue:notification.badge];
38+
[OneSignalBadgeHelpers updateCachedBadgeValue:notification.badge];
3939

4040
return;
4141
}
@@ -50,17 +50,11 @@ + (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request
5050

5151
replacementContent.badge = @(currentValue);
5252

53-
[OneSignalExtensionBadgeHandler updateCachedBadgeValue:currentValue];
53+
[OneSignalBadgeHelpers updateCachedBadgeValue:currentValue];
5454
}
5555

5656
+ (NSInteger)currentCachedBadgeValue {
5757
return [OneSignalUserDefaults.initShared getSavedIntegerForKey:ONESIGNAL_BADGE_KEY defaultValue:0];
5858
}
5959

60-
+ (void)updateCachedBadgeValue:(NSInteger)value {
61-
// Since badge logic can be executed in an extension, we need to use app groups to get
62-
// a shared NSUserDefaults from the app group suite name
63-
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:value];
64-
}
65-
6660
@end

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotification+OneSignal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ - (void)complete:(OSDisplayableNotification *)notification {
7777
*/
7878
if (!notification) {
7979
NSInteger previousBadgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
80-
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:previousBadgeCount];
80+
[OneSignalBadgeHelpers updateCachedBadgeValue:previousBadgeCount];
8181
}
8282
if (_completion) {
8383
_completion(notification);

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ + (void)clearBadgeCount:(BOOL)fromNotifOpened fromClearAll:(BOOL)fromClearAll {
795795

796796
if (_disableBadgeClearing && !fromClearAll) {
797797
// The customer could have manually changed the badge value. We must ensure our cached value will match the current state.
798-
[OneSignalUserDefaults.initShared saveIntegerForKey:ONESIGNAL_BADGE_KEY withValue:[UIApplication sharedApplication].applicationIconBadgeNumber];
798+
[OneSignalBadgeHelpers updateCachedBadgeValue:[UIApplication sharedApplication].applicationIconBadgeNumber];
799799
return;
800800
}
801801

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ + (void)load {
809809
We swizzle the 'setApplicationIconBadgeNumber()' to intercept these calls so we always know the latest count
810810
*/
811811
- (void)onesignalSetApplicationIconBadgeNumber:(NSInteger)badge {
812-
[OneSignalExtensionBadgeHandler updateCachedBadgeValue:badge];
812+
[OneSignalBadgeHelpers updateCachedBadgeValue:badge];
813813
[self onesignalSetApplicationIconBadgeNumber:badge];
814814
}
815815

0 commit comments

Comments
 (0)