setApplicationIconBadgeNumber(0) followed by cancelAllLocalNotifications() in both application(_:didFinishLaunchingWithOptions:) and application(_:didReceiveRemoteNotification:) to clear stale notifications. For remote notifications that wake the app from background, briefly increment then decrement the badge (1 then 0) to force a refresh.Why this happens
iOS Notification Center does not expose a dedicated “clear notifications for this app” API. Instead, Apple repurposed the app icon badge number as the control mechanism: when the badge goes to zero, the system treats it as a signal to remove that app’s entries from Notification Center. This design dates back to the introduction of Notification Center in iOS 5 and remains the documented approach today.
The confusion arises because setApplicationIconBadgeNumber(_:) sounds like it only affects the red number on the app icon. In practice, the SpringBoard process that manages Notification Center watches the badge value for each bundle identifier. A transition to zero triggers a cleanup pass for that app’s section in Notification Center. Local notifications are a separate queue managed by the same UIApplication instance, so cancelAllLocalNotifications() (or the UserNotifications equivalent) is needed to avoid orphaned local entries.
If the app is launched from a notification while in the background or suspended, application(_:didReceiveRemoteNotification:fetchCompletionHandler:) (or the legacy application(_:didReceiveRemoteNotification:)) fires after the notification has already been displayed. Simply setting the badge to zero at that point sometimes races with the system’s own badge update from the push payload, leaving the notification visible. The workaround is to force a visible change — increment to 1, then immediately decrement to 0 — so SpringBoard registers a state transition and processes the cleanup.
How to fix it
1. Reset badge and cancel local notifications on every launch (modern UserNotifications API)
Use the UserNotifications framework introduced in iOS 10. This is the current, non-deprecated path and works for both local and remote notifications.
import UserNotifications
func clearAllNotifications() {
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UIApplication.shared.applicationIconBadgeNumber = 0
}
// In AppDelegate or UIApplicationDelegateAdaptor
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
clearAllNotifications()
return true
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
clearAllNotifications()
completionHandler(.newData)
}
removeAllDeliveredNotifications() clears the Notification Center list; removeAllPendingNotificationRequests() cancels scheduled local notifications. Setting applicationIconBadgeNumber = 0 ensures the badge reflects the cleared state.
2. Legacy UIKit path (pre-iOS 10 or Objective-C codebases)
If you maintain an older codebase that has not migrated to UserNotifications, the original UIKit calls still work but are formally deprecated.
- (void)clearNotifications {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self clearNotifications];
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self clearNotifications];
}
3. Force badge transition for background launches
When a remote notification wakes the app from the background, the system may have already set the badge to the value from the push payload (often 1). A straight 0 assignment can be ignored if the value is already 0 or if the system coalesces the change. Briefly setting the badge to 1 forces a state change that SpringBoard observes.
func forceBadgeReset() {
UIApplication.shared.applicationIconBadgeNumber = 1
UIApplication.shared.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
forceBadgeReset()
completionHandler(.newData)
}
Call this only in the remote-notification handler, not on every launch, to avoid a momentary badge flash for the user.
4. Clear notifications when the user opens the app from the Notification Center (SwiftUI lifecycle)
For apps using the SwiftUI App protocol with UIApplicationDelegateAdaptor, place the cleanup in the delegate methods as shown above. If you rely solely on scene(_:willConnectTo:options:) or onOpenURL, you will miss cold launches from notifications. The delegate methods remain the reliable entry points.
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
UIApplication.shared.applicationIconBadgeNumber = 0
return true
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
UIApplication.shared.applicationIconBadgeNumber = 1
UIApplication.shared.applicationIconBadgeNumber = 0
completionHandler(.newData)
}
}
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene { WindowGroup { ContentView() } }
}
How to prevent it
- Always pair
removeAllDeliveredNotifications()withapplicationIconBadgeNumber = 0— one clears the list, the other clears the badge and signals SpringBoard. - Call the cleanup in both
didFinishLaunchingWithOptions(cold launch) anddidReceiveRemoteNotification:fetchCompletionHandler:(background wake). - Migrate from
UILocalNotification/cancelAllLocalNotifications()toUNUserNotificationCenterAPIs; the legacy calls are deprecated and may be removed. - Test on a physical device — the simulator does not always reflect Notification Center behavior accurately.
- If your server sends
badge: 0in the push payload, the system will clear the badge automatically, but you should still callremoveAllDeliveredNotifications()to wipe the history.
FAQ
Does setting the badge to zero delete the push notification from Apple’s servers?
No. It only removes the local representation in Notification Center and the badge on the home screen. The push payload has already been delivered; Apple Push Notification service does not support recall.
Why do notifications still appear after I call removeAllDeliveredNotifications()?
If the app is in the foreground when the push arrives, the system may display a banner but not add an entry to Notification Center (depending on user settings). removeAllDeliveredNotifications() only affects entries that are already in the center. Ensure you also handle UNUserNotificationCenterDelegate methods if you want to suppress the banner.
Can I clear only one specific notification instead of all of them?
Yes. Use UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers:) with the apns-collapse-id or your custom UNNotificationRequest identifier. You must track those identifiers when you schedule or receive notifications.
Is the badge increment-then-decrement trick still necessary on iOS 17+?
In most cases, a single applicationIconBadgeNumber = 0 suffices. The increment trick remains a safe fallback for edge cases where the system has already set the badge to 1 from the push payload and ignores a redundant zero assignment.
Do I need to request notification permissions before clearing notifications?
No. removeAllDeliveredNotifications() and applicationIconBadgeNumber work regardless of authorization status. They only affect data already stored on the device for your bundle identifier.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.
