Skip to content

Commit 50ece19

Browse files
committed
fix(badges): Use hasBadge flag to indicate badge count is set
* This fixes a bug when a notification had "SetTo" = 0; our check for the existence of notification.badge could not differentiate null vs 0, and we detected 0 as lack of existence. * Using a flag let's us differentiate between null vs 0 badge count. The payload from APNS will be already be an NSNumber: https://developer.apple.com/documentation/usernotifications/unnotificationcontent/badge?language=objc and we will set the flag if there is a badge
1 parent 3af1ea8 commit 50ece19

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSNotification.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
@property(readonly, nullable)NSString* category;
6060

6161
/* The badge assigned to the application icon */
62+
/// Indicates if badge count is set on this notification; this flag is needed as the `badge` property is an
63+
/// integer primitive and cannot be used to differentiate between null badge vs badge count of 0.
64+
@property(readonly)BOOL hasBadge;
6265
@property(readonly)NSInteger badge;
6366
@property(readonly)NSInteger badgeIncrement;
6467

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OSNotification.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ - (void)parseCommonOneSignalFields:(NSDictionary*)payload {
124124

125125
- (void)parseApnsFields {
126126
[self parseAlertField:_rawPayload[@"aps"][@"alert"]];
127+
_hasBadge = _rawPayload[@"aps"][@"badge"] ? true : false;
127128
_badge = [_rawPayload[@"aps"][@"badge"] intValue];
128129
_sound = _rawPayload[@"aps"][@"sound"];
129130
}
@@ -257,7 +258,9 @@ - (NSDictionary *)jsonRepresentation {
257258
if (self.templateId)
258259
[obj setObject:self.templateId forKeyedSubscript: @"templateId"];
259260

260-
if (self.badge)
261+
[obj setObject:@(self.hasBadge) forKeyedSubscript: @"hasBadge"];
262+
263+
if (self.hasBadge)
261264
[obj setObject:@(self.badge) forKeyedSubscript: @"badge"];
262265

263266
if (self.badgeIncrement)

iOS_SDK/OneSignalSDK/OneSignalExtension/OneSignalExtensionBadgeHandler.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ + (void)handleBadgeCountWithNotificationRequest:(UNNotificationRequest *)request
3434
//if the user is setting the badge directly instead of incrementing/decrementing,
3535
//make sure the OneSignal cached value is updated to this value
3636
if (!notification.badgeIncrement) {
37-
if (notification.badge)
37+
if (notification.hasBadge)
3838
[OneSignalExtensionBadgeHandler updateCachedBadgeValue:notification.badge];
3939

4040
return;

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ + (UNNotificationRequest*)prepareUNNotificationRequest:(OSNotification*)notifica
985985
else
986986
content.sound = UNNotificationSound.defaultSound;
987987

988-
if (notification.badge != 0)
988+
if (notification.hasBadge)
989989
content.badge = [NSNumber numberWithInteger:notification.badge];
990990

991991
// Check if media attached

0 commit comments

Comments
 (0)