I've read a number of questions here on SO regarding receiving push notifications while the application is not running (more than in the background, meaning it is shut down completely). This question in particular is most helpful in figuring out how to determine if one was receiving using the launchOptions dictionary.
However, I'm very confused, and I fully admit this may be a massive oversight on my part: when my device receives a push notification for this application while the app is shut down, and I later open my application, the launchOptions dictionary is a null pointer. From the description of the accepted answer in the previously mentioned link, and other places too, I gather that I should be able to see a notification payload; however there is nothing. I am developing for iOS 5.1.1.
My only other thought is to check the number of badges on start up (greater than zero, do something...), but this seems very unreliable.
Can anyone tell me what I am missing? Thank you in advance for your help!
application:didFinishLaunchingWithOptions: will only be called with payload information when app is launched due to notification. E.g. this could happen if user taps over notification alert (added in notification center) or notification received with content-avialble = 1 in payload (Newsstand notification) & provided your app is not in foreground as well in background.
If your app receives notification when app is in background. If it is Newsstand notification or if user taps over action button of alert below method is called
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
with [[UIApplication sharedApplication] applicationState] not equal to UIApplicationStateActive.
In above case if user does not tap over action button of notification alert and launch app by tapping over it, neither didFinishLaunchingWithOptions or didReceiveRemoteNotification is called.
If your app receives notification while in foreground didReceiveRemoteNotification is called [[UIApplication sharedApplication] applicationState] will be equal to UIApplicationStateActive.
For badge in notification, if your app is not running no code is executed and the badge is incremented by 1 in app icon. When you launch app (tap on app icon) didFinishLaunchingWithOptions is called with normally. (If app is in background or foreground when notification received, same as above)
So I think this covers every possible case. Also note that the background case is valid for iOS SDK >= 4.0
Related
Is there any way, when a UILocalnotification is displayed as an alert, for there to only be one action button displayed, rather than both "Close" and "Launch"?
Assuming there is no way, I can change the second action button to say "Continue". I want to be able to register in my app that they did press "Continue" but I don't want the app to actually launch. Is this possible?
It's not possible within current SDK, unfortunately. The push notification alert can have only two actions – either it's cancelled and your application is not notified, or your application is being brought to foreground to handle the notification.
Does didFinishLaunchingWithOptions happens after:
applicationWillResignActive
applicationDidEnterBackground
applicationWillEnterForeground
Or does it happen only after applicationWillTerminate?
And when applicationDidBecomeActive happens then? Thanks.
From the docs:
It is called after your application has been launched and its main nib
file has been loaded. At the time this method is called, your
application is in the inactive state. At some point after this method
returns, a subsequent delegate method is called to move your
application to the active (foreground) state or the background state.
It happens when the user opens your app. Followed by applicationDidBecomeActive when the app is ready to receive user events.
When the user presses the home button the following methods are called (by this order):
- applicationWillResignActive
- applicationDidEnterBackground
When the user opens your app again, and it is in background:
applicationWillEnterForeground
applicationDidBecomeActive
Finally, applicationWillTerminate is called instead of applicationDidEnterBackground on devices with iOS 3.x or earlier. Or with devices that do not support background apps (like the 3G).
application:didFinishLaunchingWithOptions:
only fires once: when your program starts up. You should typically create the main window/view controller here.
Ok, I got this issue: I have an application with a login screen that is suposed to show everytime the app goes to background and return. The problem is, the previous screen appears for a fraction of second after the app return to foreground, because the system only refresh what is being seen after loading. What is need is a complete transition before the app returns to foreground. Yes, I am doing the transition on app delegate, at applicationDidEnterBackground. tried at every single other back/fore transition method, same results. The code works fine, but theres a flash of the screen before the login screen showing up.
The full code is as follows:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (!([LogicCore loadPass] == nil || [[LogicCore loadPass] isEqualToString:#""])) //a password is set,
{
[self.window.rootViewController dismissModalViewControllerAnimated:YES];//go back to the rootview, the login screen
}
}
I forgot about this, but you can have your app exit when the user backgrounds it. The only real problem here is they see your splash screen again while the app loads.
To have your app exit when backgrounded (suspended) put the key "Application does not run in background" - raw key: UIApplicationExitsOnSuspend to YES.
Not an ideal solution, but the only one I can find at the moment.
Try making the call in applicationWillEnterForeground: which "lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active."
You're doing it after the transition has occurred (the change will be "queued" to happen after displaying the view)—you want to do it before, so you go with the will methods.
I am writing an alarm clock app.
Please correct me if I am wrong:
On both events (lock & home button in iOS 4.x) the applicationWillResignActive: method is called. When locked my app can keep on running (forever if DeepSleepPreventer.h is used) to check if the alarm should go off. When home is pressed it has to stop working at some time (apart from some basic background calculations). So in this case I have to set a local UILocalNotification to trigger the alarm.
So my question: How to differentiate between those two events?
Thank you!
Have you tried implementing -applicationDidEnterBackground: in your app delegate?
I wanted to know whether it is possible to load the application or some specific page on clicking the "OK" button of the push notification or not in objective c.
As i want to load the application when i press the button of the push notification.Kindly help me out if possible.
Thanking you.
the push notification usually have a "View" button that will automatically launch your application. you don't have to do anything in your app for that to work.
when the user launches the application that way, your app delegate receives a call to
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,
the launchOptions dictionary containing whatever data was added to the push notification payload by the server.
you can check that and open any relevant page or view, or handle that notification anyway you want.