UILocalNotification not doing anything

This may seem like a silly question, but this is my first time using UILocalNotification and I can’t get it to work for a quick test. It just doesn’t do anything.

1. I’ve created 2 variables in the AppDelegate

  • How to set UNAlertStyle?
  • UILocalNotification - Fire and Repeat at particular time each day
  • Scheduled Local Notifications showing up when toggled ON for time period when toggled OFF
  • UILocalNotification with various alert body
  • UIApplication scheduledLocalNotifications is empty for past non-repeating notifications
  • How to set an Alarm in iOS?
  • let today = NSDate()
    let notification: UILocalNotification = UILocalNotification()
    

    2. Then in the applicationDidEnterBackground function, I have the following

        notification.fireDate = today.dateByAddingTimeInterval(10)
        notification.alertTitle = "My App Test"
        notification.alertBody = "Testing Notification \n :)"
        UIApplication.sharedApplication().presentLocalNotificationNow(notification)
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    

    3. Also added this to the applicationDidBecomeActive function

    UIApplication.sharedApplication().cancelAllLocalNotifications()
    

    Solutions Collect From Internet About “UILocalNotification not doing anything”

    After reading the documentation again, I realized I missed a crucial first step which is to register my App first for user notifications. The Apple doc was written in OBJ-C, but I was able to figure it out in order to convert it to swift. This is what I did:

    1. I added this to my AppDelegate didFinishLaunchingWithOptions function and it now works

    var types: UIUserNotificationType = UIUserNotificationType()
    types.insert(UIUserNotificationType.Alert)
    types.insert(UIUserNotificationType.Badge)
    
    let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
    
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)