2016-03-14 90 views
0

我不知道我在做什麼錯。我有以下代碼:本地通知不適用於iOS模擬器

//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,它永遠不會被調用。

+0

當本地通知開啓,關閉應用程序,然後檢查。 – iphonic

+0

你如何打開通知?模擬器的設置應用程序中沒有通知設置。 – u84six

回答

3

好吧,我會回答我自己的問題。在AppDelegate中中:didFinishLaunchingWithOptions,我必須註冊,像這樣的通知:

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