when I implementation the UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
var status = ""
if (UIApplication.shared.applicationState == .active) {
status = "active"
} else if (UIApplication.shared.applicationState == .background) {
status = "background"
} else if (UIApplication.shared.applicationState == .inactive) {
status = "inactive"
}
completionHandler()
}
I find that UIApplication.shared.applicationState == .background this case can not execute when application is in background。
why applicationState is inactive not background?
First let me explain what the .inactive
state means in the app life cycle.
Despite it might get interpreted as "the app is not running", actually .inactive
is a state between the app being in the foreground (.active
) and background, This is a short lived state while the app is in transition between being foreground and background.
If your app has implemented the various app delegate callback functions, you would see calls to applicationWillResignActive(_:)
and applicationDidBecomeActive(_:)
while the app is transitioning.
If your app does not implement scenes, the .applicationState
property will show `.inactive' when the app is being transitioned as I described.
If your app is scene-based, then this property has a different meaning. A scene-based app launches in the background state, and transitions between its states as scenes connect, change their states, and disconnect. This property will show the value of the most active scene, instead.
For scene-based apps, you should use UISceneDelegate to respond to changes in an individual scene’s life cycle.
Argun Tekant / DTS Engineer / Core Technologies