簡體   English   中英

本地通知不適用於iOS模擬器

[英]Local notification not working with iOS simulator

我不確定自己在做什么錯。 我有以下代碼:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM