UILocalNotification没有做任何事情

这可能看起来像一个愚蠢的问题,但这是我第一次使用UILocalNotification,我不能得到它的工作进行一个快速的testing。 它只是没有做任何事情。

1.我在AppDelegate中创build了两个variables

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

再次阅读文档后,我意识到我错过了一个关键的第一步,首先注册我的应用程序用户通知。 苹果文档是用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)