这可能看起来像一个愚蠢的问题,但这是我第一次使用UILocalNotification,我无法让它用于快速测试。它只是没有做任何事情。
1。我在AppDelegate中创建了2个变量
let today = NSDate()
let notification: UILocalNotification = UILocalNotification()
2。然后在applicationDidEnterBackground函数中,我有以下
notification.fireDate = today.dateByAddingTimeInterval(10)
notification.alertTitle = "My App Test"
notification.alertBody = "Testing Notification \n :)"
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
第3。还将此添加到applicationDidBecomeActive函数
UIApplication.sharedApplication().cancelAllLocalNotifications()
答案 0 :(得分:5)
再次阅读文档后,我意识到我错过了关键的第一步,即首先注册我的应用程序以获取用户通知。 Apple doc是用OBJ-C编写的,但我能够弄清楚它是为了将它转换为swift。这就是我所做的:
<强> 1。我将此添加到我的AppDelegate didFinishLaunchingWithOptions函数中,现在可以正常使用
var types: UIUserNotificationType = UIUserNotificationType()
types.insert(UIUserNotificationType.Alert)
types.insert(UIUserNotificationType.Badge)
let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)