Understanding app background launch behavior - ios

In apple Doc Understanding When Your App Gets Launched into the Background
Apps that support background execution may be relaunched by the system
to handle incoming events...
I am doing region monitoring and when I get that I am popping a UILocalNotification, but when I tap on UILocalNotification my app didReceiveLocalNotification is called. May be because my app is launched in background.
Second thing I did is I did not tap UILocalNotification and left for few minutes, means my app will terminate by iOS. I drag the notification center and then tap the UILocalNotification still my app enter in didReceiveLocalNotification.
The behavior I am expecting that app now launch in action of delayed UILocalNotification tap must enter in this method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSLog(#"notification caused app to launch, alert body = %#", notification.alertBody);
// do what ever action you want to do
// you could just copy the code from "didReceiveLocalNotification" and paste it here
}
return YES;
}
What is actually happening can please any one explain in detail?
Even after a delay when I tap UILocalNotification app do not enter in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Your app isn't terminated after a few minutes of inactivity.It becomes idle, and only terminated if memory is needed.Remember that it is launched to process the region change in the background, and it sends the notification in the first place, so it's resident in the device memory in this stage.Try to explicitly quiting it using the multitasking interface or launch several memory consuming apps (games usually do the trick) to force it to quit.A few minutes of waiting in the background won't make it terminate.

Related

Local Notifications with iOS 8

Local notification are working well in foreground. but I need to perform some background work when a notification banner displays. It is working well when I tap on the banner when local notifications appear.
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
NSLog(#"AppDelegate didFinishLaunchingWithOptions");
[self application:application didReceiveLocalNotification:notification];
}
return YES;
}
My problem is similar to this issue
I used code below to perform background task:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[[ReminderManger sharedMager] handleNotification:[[notification userInfo] objectForKey:#"reminderId"]];
});
}
There is no way to do that. Notifications are handled by iOS itself. If app would be handling this, there might be some way to get this figured out. I've been wasting my time over it but didn't succeed.
The only thing you can do is to get extended permission from iOS that your app need multi tasking in background. And then in background make your app become post local notifications, this will show banner whenever you want but you can't customize the banner. See Apple's line "An app that is running in the background may also schedule a local notification to inform the user of an incoming message, chat, or update." at this link
Now the thing is in background, you cant change your views, but parse data, and based on this you can make changes at time of launch.

Handling local notifications when the user presses the icon instead of the alert

Here is the situation that I want to handle quoted from Apple's documentation.
As a result of the presented notification, the user taps the action button of the alert or taps (or clicks) the application icon.
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification . If the application icon is clicked on a computer running OS X, the application calls the delegate’s applicationDidFinishLaunching: method in which the delegate can obtain the remote-notification payload.
How do I handle this situation if there is no information about the notification?
If I understand you correctly, it sounds like you have a UILocalNotification that has been fired, but you need to still handle it if the user taps the application icon instead of the notification. Correct?
If this is the case, then to my knowledge you won't be able to handle the notification from the app delegate, because the app is not being launched or brought out of the background by the notification, but instead by the user's interaction.
However, if you are setting a badgeNumber on the application with the notification then you could try something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
} else if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
// Assume that user launched the app from the icon with a notification present.
}}
You may also have to check the badgeNumber in - (void)applicationDidBecomeActive:(UIApplication *)application as well.
Improve to #Aron Crittendon answer:
Consider also to handle that in applicationDidBecomeActive:
-(void)applicationDidBecomeActive:(UIApplication *)application
{
if ([UIApplication sharedApplication].applicationIconBadgeNumber > 0) {
//application is in background, fired notification and user tapped app icon with badge
}
}
As the documentation states, if you tap the icon on iOS (and not the notification's alert/banner) then the same method is called but you get no notification information. There is no way to handle a local notification simply by tapping the app icon.

Launch Closed iOS App From Local Notification

When my iOS application is running in the background it responds fine to
- (void)application:(UIApplication *)application didReceiveLocalNotification:
(UILocalNotification *)notification
but when the application is closed it crashes and gives a SIGKILL error.
How can I run a method within the app if it is closed when the notification is received?
When you app is closed then when you get notification than on click of notification - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method is called.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
// code here.
}
You can't run a method in the app when a local notification is received. The notification can provide any combination of an alert, icon badge number, and a sound (<30 secs).
You can run a method when it comes into the foreground again either through the notification or through other means.
When the app is in the background it will call applicationWillEnterForeground: prior to resuming. You can override this method to handle anything needed after the notification. You can override applicationDidEnterBackground: to determine when your app actually enters the background.
Method application:didReceiveLocalNotification: is called when the app receives a notification but is in the foreground. The alert, icon badge number, and sound will not be triggered when the app is in the foreground.

iOS / XCode: how to know that app has been launched with a click on notification or on springboard app icon?

I would like to know if there is a way to know if an app (which can be closed or open in background) has been launched with a click on:
a notification (in the notification center) ?
or the app icon on the springboard ?
Thanks !!
put this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
// launched from notification
} else {
// from the springboard
}
}
in your UIApplicationDelegate.
From Apple Docs on Scheduling, Registering, and Handling Notifications
:
iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.

iOS: Handling Remote (push) Notifications

I'm trying to handle all possible cases with remote notifications.
I'm ok when app in foreground - didReceiveRemoteNotification is called.
The problem is when app in background state, and I receive push notification.
Nothing is called.
how to let user know that he has new remote notification when app come back to foreground ?
The only way for you to intercept a push notification is when the user tap the notify in the notification center (or when slide the app icon from the lock screen).
In this case before the app go in foreground the didFinishLaunchingWithOptions method in the app delegate is called. You should use the NSDictionary launchOptions to determine if the app has been launched from notification center or by tapping the icon (the normal use)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushDic = [launchOptions objectForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (pushDic != nil) {
NSLog(#"Notification");
}
else {
}
}
Just have a look into the programming guide:
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
Of course, if your app is in background, there will be nothing called...
If your app is not launched (not even suspended in background), the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will contain the notification payload (key UIApplicationLaunchOptionsRemoteNotificationKey):
NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

Resources