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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With