Reputation: 1861
I am trying to remove the icon badge in swift, but PFInstallation doesn't seem to work anymore. How do I do this?
Upvotes: 109
Views: 69645
Reputation: 92
As of iOS 17.0, you have to use:
UNUserNotificationCenter.current().setBadgeCount(0)
Upvotes: 0
Reputation: 960
A more SwiftUI-oriented approach might be to listen for changes in the @Environment(\.scenePhase) var scenePhase
in the root view. Then, if the new phase is .active
, set UIApplication.shared.applicationIconBadgeNumber
to 0
as discussed by the other answers.
Example Code:
@main
struct MRPApp: App {
@Environment(\.scenePhase) var scenePhase
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
UIApplication.shared.applicationIconBadgeNumber = 0
}
}
}
}
}
Upvotes: 7
Reputation: 817
Swift 5
While you can put this in the AppDelegate didFinishLaunchingWithOptions, this will not clear the badge if the app is inactive and has moved to active.
If you wish to clear the badge regardless of the previous state you need to put this in the SceneDelegate instead of the AppDelegate.
func sceneDidBecomeActive(_ scene: UIScene) {
UIApplication.shared.applicationIconBadgeNumber = 0
}
Upvotes: 18
Reputation: 5502
Swift 5
At the AppDelegate didFinishLaunchingWithOptions
UIApplication.shared.applicationIconBadgeNumber = 0
Upvotes: 17
Reputation: 339
Swift 4.2
At the AppDelegate, just put this code:
func applicationDidBecomeActive(_ application: UIApplication) {
application.applicationIconBadgeNumber = 0
}
Upvotes: 28
Reputation: 6604
You can "remove" the app badge icon by setting it to 0:
Swift < 3.0
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
Swift 3.0+
UIApplication.shared.applicationIconBadgeNumber = 0
This question shows when you can use it: How to clear push notification badge count in iOS?
Upvotes: 280