In my first SwiftUI app, I have Remote Notifications and Background Processes enabled. I did add an AppDelegate class, to support notification.
The notifications set the app badge to an appropriate value.
Since this app has these background modes enabled, several lifecycle events are not working:
applicationDidBecomeActive
applicationWillResignActive
applicationDidEnterBackground
applicationWillEnterForeground
Question: where/how do I reset the badge?
Here is how you can observe didBecomeActiveNotification
:
@main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
UIApplication.shared.applicationIconBadgeNumber = 0
}
}
}
}
You can observe other notifications in the same way.
Alternatively you can use an @EnvironmentObject
to track the application state: