0

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.

wackytacky99
  • 614
  • 1
  • 14
  • 33

2 Answers2

0

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.

Manu Jose
  • 131
  • 2
  • 18
  • Can you explain in terms on code how can I achieve this. Where & when should the cancelLocalNotitification method be called. – wackytacky99 Aug 11 '15 at 16:13
0
  • (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.

Munahil
  • 2,381
  • 1
  • 14
  • 24