3

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

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()
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • You are scheduling the notification to be 10 seconds after you create the `today` variable. Are you actually putting the app in the background within 10 seconds of starting it? Did you add all of the needed code to enable and respond to notifications? Did you accept permission to allow notifications for your app? – rmaddy Sep 30 '15 at 14:40
  • Yes, as soon as I launched my app, I put it in the background right away to make sure I was waiting 10 secs. I even saw my log statements making the calls. In response to your second question, I did find it odd that my App never asked for permission to allow notifications. But thought nothing of it yet. How do I force it to ask? – Lavvo Sep 30 '15 at 14:42
  • 1
    You should read the [Local and Remote Notification Programming Guide](https://developer.apple.com/library/prerelease/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html#//apple_ref/doc/uid/TP40008194) for details. – rmaddy Sep 30 '15 at 14:42
  • Remove `UIApplication.sharedApplication().presentLocalNotificationNow(notification)`. – Midhun MP Sep 30 '15 at 14:49
  • @rmaddy is correct. I just read the documentation again and missed a crucial first step which is to register my App first for user notifications. After I did this it is now working. I'll add my answer. – Lavvo Sep 30 '15 at 14:51

1 Answers1

5

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)
Lavvo
  • 1,024
  • 3
  • 16
  • 35
  • Jesus Christ, I couldn't get it to work and no one mentioned this any where... Thank you! – Atomix Oct 20 '15 at 20:45
  • 1
    I agree, all the examples I've seen never mention this first step. But glad I was able to figure it out anyway and help anyone else who needs it. – Lavvo Oct 20 '15 at 22:12