UILocalNotification not doing anything

一笑奈何 提交于 2020-01-13 16:30:11

问题


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

回答1:


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)


来源:https://stackoverflow.com/questions/32868884/uilocalnotification-not-doing-anything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!