Skip to content

fix(badges): drop deprecated applicationIconBadgeNumber usage #1568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 29, 2025

Conversation

nan-li
Copy link
Contributor

@nan-li nan-li commented May 28, 2025

Description

One Line Summary

Drop usage of deprecated applicationIconBadgeNumber API, and support usage of replacement setBadgeCount API (with no getter).

Details

Motivation

Reports of the SDK's usage of applicationIconBadgeNumber is causing apps to hang: #1452

Scope

Note the biggest change is we lose some ability to get the most accurate badge count at any moment. This should be mitigated overall as we swizzle both badge setting methods that developers would call (old and new) to update our cached value. Additionally, we will store a previous badge count as well for cases where a notification display event is cancelled, so we can revert our cached badge count to be accurate.

New class OneSignalBadgeHelpers in OneSignalCore and move a method to cache badge count out of OneSignalExtension and into that class

  • Moving to Core means the functionality can be shared across different frameworks

Support new setBadgeCount API on iOS 16+.

Remove all usages of deprecated applicationIconBadgeNumber

  • 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 badge count. Thus, we store the previous badge when a new one is set as we cannot use a getter for badge count anymore.

Testing

Unit testing

None

Manual testing

iPhone 13 iOS 18.5

  • manually called setBadgeCount and check SDK swizzling and caching
  • Display notifications in foreground as well as cancel displaying notifications in foreground, and check caching
  • Sending notifications with different combinations of "SetTo" and "Increase" badge count values and checking behavior is correct

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
    • Badges
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
  • I have reviewed this PR myself, ensuring it meets each checklist item

This change is Reviewable

nan-li added 6 commits May 27, 2025 22:55
* The `clearBadgeCount` was returning a bool flag to indicate if badge was set within the method. However, nothing is consuming this flag, and with the Apple API to get application icon badge number deprecated (https://developer.apple.com/documentation/uikit/uiapplication/applicationiconbadgenumber?language=objc), the SDK's ability to get accurate badge count at any particular moment is reduced.
* 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.
* currentCachedBadgeValue is only used within the OneSignalExtensionBadgeHandler class, no need to expose on header.
* The method `applicationIconBadgeNumber` is deprecated in iOS 17 - see https://developer.apple.com/documentation/uikit/uiapplication/applicationiconbadgenumber
* Its replacement is `setBadgeCount:withCompletionHandler:` but there is no equivalent getter.
* Use this new API on ios 16+ and swizzle it just like we do for applicationIconBadgeNumber
* applicationIconBadgeNumber is deprecated and reports of apps hanging have been submitted, let's move off it.
* In clearBadgeCount, we read if badge was > 0 before setting badge to 0. Since we can't read accurately, let's just always set to 0, which is what the old logic effectively did anyways.
* 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.
@nan-li nan-li requested review from jkasten2 and jinliu9508 May 28, 2025 08:52
Base automatically changed from fix/set_badge_number_to_zero_bug to main May 29, 2025 03:13
@nan-li nan-li merged commit b20caa9 into main May 29, 2025
1 of 2 checks passed
@nan-li nan-li deleted the fix/drop_applicationIconBadgeNumber_usage branch May 29, 2025 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants