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

1

9
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?

Marna answered 19/4, 2015 at 22:59 Comment(2)
you mean, how to detect if the app is opened but there is an unopened notification hanging?Paroxysm
@Paroxysm yes, I guess so.Marna
F
20

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.

Feeney answered 19/4, 2015 at 23:8 Comment(3)
I'm guessing "applicationDidBecomeActive" is what I'm looking for right?Marna
Probably. It's easy enough to put break points or println in all of them and play around to see exactly how the life cycle works and figure out what you need exactly.Feeney
According to the reference pages there is a dictionary with keys in which you can find if there are still notifications hanging: developer.apple.com/library/ios/documentation/UIKit/Reference/…Paroxysm

© 2022 - 2024 — McMap. All rights reserved.