[英]Local notification not working with iOS simulator
I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 I have the following code:
我有以下代码:
//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)
When I place a breakpoint in application: didReceiveLocalNotification in app delegate, it never gets called. 当我在应用程序中放置一个断点时:在应用程序委托中执行didReceiveLocalNotification,就永远不会调用它。
Ok, I'll answer my own question. 好吧,我会回答我自己的问题。 In AppDelegate: didFinishLaunchingWithOptions, I had to register notifications like so:
在AppDelegate:didFinishLaunchingWithOptions中,我必须这样注册通知:
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.