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

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.

Related

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.

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

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.

Is it possible to handle the selected local notification when activing the app from iOS notification panel?

When launching the app from a local notification you can use the application:didFinishLaunchingWithOptions: method of ApplicationDelegate to handle the notification, but this method is not called when you select the notification in the notifications panel and the app is in background.
Is there a way to handle the notification in that case?
I want to show a special viewcontroller when the app becomes active by this cause.
Thanks in advance!
Did you check the below delegate method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

What method is called when a push notification is accepted?

I'm trying to perform some task when a user gets a push notification and presses VIEW. I don't want the app to just launch like it normally does, I want to perform some other task.
What method gets called when the user gets a push and presses VIEW?? didFinishLaunchingWithOptions doesn't seem to get called (not on iPhone 4 anyways).
I basically want something to happen when a user presses VIEW, and only when they press VIEW.
Thanks.
Are you sure application:didFinishLaunchingWithOptions: is not being called? Normally you can tell that your app was launched from the user clicking on the view button of a remote push notification by inspecting the launchOptions parameter.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (pushInfo)
{
// app was launched from a remote push notification
}
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

Resources