Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check when an app comes back into the foreground from the background, but not from push notification?

func application(application: UIApplication, didReceiveRemoteNotification data: [NSObject : AnyObject]) {
        var dat = JSON(data)
        if application.applicationState == UIApplicationState.Active {
            // app was already in the foreground
            println("App is in foreground")
            processNotification(dat)
        }else{
            // app was just brought from background to foreground via PUSH
            println("App brought back via PUSH")
            processNotification(dat)
        }
    }

This is how I check for push notifications. However, when if I send a push notification, the user misses it, and then opens the app via the icon? How can I check when the app was opened from the icon?

like image 687
TIMEX Avatar asked Apr 19 '15 22:04

TIMEX


People also ask

How do you know when an app moves to the foreground?

It's very easy to detect when an Activity goes background/foreground just by listening to the lifecycle events, onStop() and onStart() of that Activity.

How do you detect when an Android app goes to the background and come back to the foreground?

The onPause() and onResume() methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.

Why is an app running in the foreground?

Running in the Foreground means your app is currently Fully Visible on your device, you can see it and interact with it and it will respond to you right away.

What is the state of an app that is running in the foreground but is not receiving any events suspended?

Inactive – The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received. Active – The app is running in the foreground, and receiving events. Background – The app is running in the background, and executing code.


1 Answers

The UIApplicationDelegate protocol defines several methods that let you add code to several of your application's life cycle events.

Of specific interest to you would be the following:

  • application(_:willFinishLaunchingWithOptions:) - called just before the application has finished launching when the application was not already active in the background
  • application(_:didFinishLaunchingWithOptions:) - called just after the application has finished launching when the application was not already active in the background
  • applicationDidBecomeActive(_:) - called just after the application has become active, this is called when the user launches from scratch, reopens from background, and also when a user returns from a temporary interruption (such as a phone call)
  • applicationWillEnterForeground(_:) - this is called just before the application enters the foreground after having been in the background--it is immediately followed by the applicationDidBecomeActive(_:) call

This life cycle events can fire whether the user opened your application via a notification or by tapping on the icon. As far as I know there is no way to tell for certain that the application was opened via tapping the icon. You can know(ish) that the application wasn't opened via a notification, as the relevant "did receive notification" methods will never fire. But this still allows the user two (at least) methods of opening the application: tapping the app icon or double tapping the home button and tapping the app to awake it from the background.

like image 178
nhgrif Avatar answered Oct 04 '22 16:10

nhgrif