Lavvo
Lavvo

Reputation: 1024

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

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()

Upvotes: 3

Views: 1210

Answers (1)

Lavvo
Lavvo

Reputation: 1024

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)

Upvotes: 5

Related Questions