Launch Closed iOS App From Local Notification - ios

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.

Related

Understanding app background launch behavior

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.

Cancel LocalNotifications method not executing when user closes/exits the app

I'm testing this on iOS 8.4 on Xcode simulator and on an iPhone 6. My notifications work fine and fire perfectly. But I can't figure out how to cancel the notifications when the user quits/closes the app. Pressing home button should NOT cancel the notifications and should still fire the notification.
This is what I have tried.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
return YES;
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
For the below method - it cancels the notifications when user presses Home button but or closes the app which is not what I want.
- (void)applicationDidEnterBackground:(UIApplication *)application
For below method - notifications will not cancel for home button or closing the app.
- (void)applicationWillEnterForeground:(UIApplication *)application
For below method - it does not execute the method at all for home button or closing the app.
- (void)applicationWillTerminate:(UIApplication *)application
I have looked into other similar questions posted on stack overflow but can't seem to get any of those suggestions to work. Please advise.
Once you fire the Notification it will be registered in the OS. OS presents the notification in time. You cant delete the notification after the application is terminated. No method will be called at the time of Termination.
When a notification arrives if your app is in foregroung didReceiveLocalNotification method will be called.
If you are in background DidLaunchwithOption method is called.
If the app is terminated no method is called.
You fire silent notification and present the actual notifications when you receive silent notifications in these methods. You can use userinfo to identify your notifications.
(void)applicationDidEnterBackground:(UIApplication *)application
Transitioning to the background, i.e when Home Button is pressed.
(void)applicationWillEnterForeground:(UIApplication *)application
Called when transitioning out of the background state
(void)applicationWillTerminate:(UIApplication *)application
Called only when the app is running. This method is not called if the app is suspended.
So, you won't be able to cancel notification, when app is about to be terminated. Therefore, you should choose some other mechanism to delete the Local Notifications. Like create a button to cancel all notifications or something like that.

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.

Is there anyway to know if users bring up app by clicking in notification center?

in method
didReceiveLocalNotification:(UILocalNotification *)notification i need to detect if it is invoked because user click on a notification in Notification Center or not to have appropriate actions.
Is there anyway?
using didReceiveLocalNotification:(UILocalNotification *)notification you cannot check if app is opened from local notification push notification or by directly clicking on the app icon.
But,
what you can do is in method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
write this code
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) {
// App opened from push notification but app was not in background
}else if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]!=nil){
// App opened from local notification and app was in background
}
and in method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
you can detect if app is opened from local notification when app was in background
and finally one last method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
In this method you can check if app is opened from push notification when app was either running or it was in backround
I guess this will help u in knowing if app is opened from notification centre or from somewhere else

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