Search code examples
iosswiftuilocalnotification

Local notifications stopped working, can't figure out why


I got local notifications working for my iOS project some time last year before putting it aside, but when I came back a few weeks ago I noticed that they no longer worked. I've dug around for a few days and I'm completely stumped. The badge still updates properly with a background fetch, but the notification alert is no longer sent. Here's a minimal setup of what I have for testing.

AppDelegate:

func application(application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
   application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
   application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Alert], categories: nil))

    return true
}    

 func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {    
   let localNotification = UILocalNotification()
   localNotification.alertAction = "Message"
   application.presentLocalNotificationNow(localNotification)

   completionHandler(.NoData)
 }

Things I've tried:

  • Uninstalling/reinstalling the app (device and simulators)
  • Changing the Bundle identifier
  • Disabling/re-enabling background fetch capability
  • Present delayed local notification by adding the fireDate attribute
  • Tried willFinishLaunchingWithOptions and didFinishLaunchingWithOptions
  • I've debugged it and am 100% certain that performFetchWithCompletionHandler executes when I simulate background fetch and that presentLocalNotificationNow is called

Setup:

  • Xcode 7.3.1 (started on previous release, probably 6.x.x)
  • Swift 2.2 (started the project on 2.1 and was functional)
  • iOS 9 (started the project on 8 and was functional)

Did something change in the API that I missed, or is there some error in what I'm doing? Thanks for any help!


Solution

  • UILocalNotification requires that alertBody be set in order to display. So having the following works:

    func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {    
       let localNotification = UILocalNotification()
       localNotification.alertBody = "You have a notification"
       localNotification.alertAction = "Message"
       application.presentLocalNotificationNow(localNotification)
    
       completionHandler(.NoData)
    }