本地通知不适用于iOS模拟器

时间:2016-03-14 20:07:52

标签: ios swift ios-simulator ios9 uilocalnotification

我不确定我做错了什么。我有以下代码:

//Create date
let calendar = NSCalendar.currentCalendar()
let now = NSDate()

let dateComponents = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: now)

dateComponents.minute += 2 //add 2 minutes to current time for debug

let date = calendar.dateFromComponents(dateComponents)

//Create notification
let notification = UILocalNotification()
notification.alertBody = "Your task is now available"
notification.alertAction = "open"
notification.fireDate = date
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["id": NSUUID().UUIDString]
notification.category = "Notification"
     UIApplication.sharedApplication().scheduleLocalNotification(notification)

当我在应用程序中放置一个断点:应用委托中的didReceiveLocalNotification时,它永远不会被调用。

1 个答案:

答案 0 :(得分:3)

好的,我会回答我自己的问题。在AppDelegate:didFinishLaunchingWithOptions中,我必须注册这样的通知:

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)