I'm trying to fire a local notification after my app gets killed to warn the user that whatever the app was doing, it can't do it anymore. MileIQ does something similar with background location tracking when warning their user that the app got killed and that they need to restart it if they want trips to still be recorded.
My code looks like this in a UIApplicationWillTerminateNotification
handler:
let notification = UILocalNotification()
notification.soundName = UILocalNotificationDefaultSoundName
notification.alertTitle = "title"
notification.alertBody = "body"
notification.fireDate = NSDate().dateByAddingTimeInterval(6)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
The magic fireDate
delay set at 6 seconds is because applicationWillTerminate
has 5 seconds to the app to cleanup, and iOS kills the app if the method doesn't return in time.
What I noticed is that if I fire the local notification instantly, it appears very briefly and gets dismissed when the app gets killed by iOS. By adding a 6 delay, the notification appears about 50% of the time and doesn't get dismissed.
What I'm looking for is a reliable solution to show this local notification after the app gets killed. Any idea how to do this?