UILocalNotification scheduled in the - (void)applicationWillTerminate:(UIApplication *)application does not get called on iOS 9 but works fine on iOS 8 and below. is any one facing similar issue.
UILocalNotification does not show up on iOS9
How are you terminating your app? On modern systems it’s rare for
-applicationWillTerminate: to get called. Rather, when the user pressed the Home button the app moves to the background (
-applicationDidEnterBackground:). The app typically gets suspended shortly after moving to the background and, once it’s suspended, can be terminated without any more code in the app running.
Share and Enjoy
—
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
Happens to me too.
On iOS8, when user killed the app by removing it from the background apps (by swiping it from the list) I received a local notification inserted in applicationWillTerminate.
But on iOS9 I'm not receiving this local notification. Something have changed.
Just FYI,
-applicationWillTerminate: is only delivered if you swipe up on an app that’s running (either in the foreground, or running in the background via a UIApplication background task or likewise). I tried this on an iOS 9 test device here in my office and it seems to behave the same way.
So, is the problem that
-applicationWillTerminate: is not being called? Or that it is being called but the notification you schedule therein is not being displayed?
Share and Enjoy
—
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
I'm also having this issue. -applicationWillTerminate: is being called, however, local notifications aren't being registered. (My app has the UIApplicationExitsOnSuspend info.plist key set to YES, so -applicationWillTerminate: is called every time the home button is pressed).
For me, as well, this started happening in iOS 9. This doesn't seem to be specific to setting notifications in -applicationWillTerminate:. When I try to register a local notification in other sections of code, it doesn't work either in iOS 9.
I should also mention that the usual "Application would like to show you notifications" alert that users normally see doesn't pop up the first time I try to add a new notification. My app also doesn't show up under Settings -> Notifications.
Here's the code I'm using for a notification:
/
NSDate *dateToSet = [NSDate dateWithTimeIntervalSinceNow:20.0];
NSString *message = @"Test notification";
UILocalNotification *warningNotification = [[UILocalNotification alloc] init];
warningNotification.fireDate = dateToSet;
warningNotification.timeZone = self.timeZone;
warningNotification.alertBody = message;
warningNotification.hasAction = NO;
warningNotification.userInfo = nil;
warningNotification.repeatInterval = 0;
warningNotification.soundName = nil;
[[UIApplication sharedApplication] scheduleLocalNotification:warningNotification];
I solved this issue by calling the following code before registering the notification on iOS 8+:
if(IS_OS_8_OR_LATER)
{
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}
I have the same issue:
-applicationWillTerminate: is called but the notification scheduled in there is not been displayed.
On iOS 8.x the same code works correctly.
I had the same problem. It used to work on iOS 8, once i upgraded to iOS 9 it doesn't work. I tried multible stuff. But when i added this line
[NSThread sleepForTimeInterval:2];After the fire notification in
applicationWillTerminateit works.
It seems u need to keep the processor busy in order to make ur nottification fired. I hope that help 🙂
thank you!
I have similar issue. My app is a reminder-type app and uses UILocalNotification.
Some users report sometimes they don't get reminders anymore (it began with iOS 9).
In the app logs I notice they receive unexpected applicationWillTerminate right once the app is moved to background.
I schedule reminders in applicationDidEnterBackground. The first thing I do is starting background task to let iOS know that there are some things to do on background. UILocalNotifications are computed and created on background thread (things are a bit complex but since the app is CoreData-based - it's actually a queue of child CoreData context started with performBlock).
So for related users the flow seems to be:
1. applicationDidEnterBackground
2. beginBackgroundTaskWithName
3. applicationWillTerminate arrives somewhere here
4. Setup UILocalNotifications
5. [app setScheduledLocalNotifications:arrReminders];
6. endBackgroundTask
It may be that when I assign reminders in #5 although my backgroundTask is still working the app probably is really terminated and it just ignores the call.
Have no idea why that happens and how it can be that endBackgroundTask not called yet (and I'm supposed to have up to 10 minutes, the whole process really takes 1-2 seconds) and the app is terminated before that.
Another idea just came to mind is maybe [app setScheduledLocalNotifications:arrReminders] should be called on main thread with
dispatch_async(dispatch_get_main_queue(), ^{})The hard part is that it works perfectly on all my test devices and even for those users who have issues it does not happen every time.
From the Local and Remote Notification Programming Guide:
In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.
As shown in the guide, the sample code that Apple shows is:
UIUserNotificationType types = UIUserNotificationTypeAlert;//or UIUserNotificationTypeBadge or UIUserNotificationTypeSound UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];This is my code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIUserNotificationType types = UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"Finally got it!";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
return YES;
}I hope that help!