Skip to content

Commit 48dd3f4

Browse files
authored
Merge pull request #1565 from OneSignal/fix/set_badge_number_to_zero_bug
fix(badges): Fix bug when notification has badge set to zero
2 parents cbf5167 + 50ece19 commit 48dd3f4

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)