UILocalNotification 没有做任何事情 [英] UILocalNotification not doing anything
问题描述
这似乎是一个愚蠢的问题,但这是我第一次使用 UILocalNotification,我无法让它快速测试.它什么都不做.
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.我在 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()
推荐答案
再次阅读文档后,我意识到我错过了关键的第一步,即首先注册我的应用程序以接收用户通知.Apple 文档是用 OBJ-C 编写的,但我能够弄清楚以便将其转换为 swift.这就是我所做的:
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.我将此添加到我的 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)
这篇关于UILocalNotification 没有做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!