swift 2中的Firedate Notification

时间:2015-12-07 19:04:43

标签: ios swift uilocalnotification

我正在尝试学习Xcode和swift,我无法通知我正在制作一个简单的闹钟。我使用了日期选择器并且通知了notification.firedate =。通知窗口会显示通知,但警报窗口没有出现且没有声音播放。

我还要求用户允许通知。请给我一些帮助。谢谢

的AppDelegate

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:  [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[ .Alert, .Badge, .Sound], categories: nil))

    return true
}

的ViewController

@IBAction func alarmSet(sender: AnyObject) {
    let alarmPicked = timePick.date
    printTime(timePick.date)
    self.notify(alarmPicked)

}
func notify(date: NSDate){
    let calendar = NSCalendar.currentCalendar()
    let comp = calendar.components([.Hour, .Minute], fromDate: date)

    let notification = UILocalNotification()
    notification.timeZone = NSTimeZone.localTimeZone()
    notification.fireDate = calendar.dateFromComponents(comp)
    notification.alertBody = "Here is the you Alarm you scheduled!"
    notification.alertAction = "Dismiss"
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
    self.navigationController?.popToRootViewControllerAnimated(true)

}

1 个答案:

答案 0 :(得分:1)

从iOS 8+开始,UIApplication要求您注册通知设置。如果你没有,它会让你安排它们,但它们永远不会开火。

您可以像这样注册您的设置:

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

然后您可以安排实际触发的通知。

请参阅Apple的SDK:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1