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?
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 backgroundapplication(_:didFinishLaunchingWithOptions:)
- called just after the application has finished launching when the application was not already active in the backgroundapplicationDidBecomeActive(_:)
- 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(_:)
callThis 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.