Determine if an app is running in the foreground when a notification is received on iOS Determine if an app is running in the foreground when a notification is received on iOS ios ios

Determine if an app is running in the foreground when a notification is received on iOS


As described in the push notification documentation you can read [[UIApplication sharedApplication] applicationState] when you receive the notification to determine whether your app is in foreground, inactive (it's visible but a dialog like the WiFi chooser is in front) or in background.


Just to have a copy-paste code available for others:

if([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive){    //App is in foreground. Act on it.}


Swift 5 version:

import UIKitlet isForeground = UIApplication.shared.applicationState == .active