在设定的时间点燃本地通知

时间:2016-05-25 19:14:05

标签: ios swift

我已经混合并匹配了教程中的代码,并且:

@IBAction func scheduleLocal(sender: AnyObject) {
    var getCurrentYear = dateFire.year
    var getCurrentMonth = dateFire.month
    var getCurrentDay = dateFire.day

    dateFire.year = getCurrentYear
    dateFire.month = getCurrentMonth
    dateFire.day = getCurrentDay
    dateFire.hour = 15
    dateFire.minute = 1
    dateFire.timeZone = NSTimeZone.defaultTimeZone()

    var calender: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date: NSDate = calender.dateFromComponents(dateFire)!

    guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return     }

    if settings.types == .None {
        let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
        return
    }

    let notification = UILocalNotification()
    notification.fireDate = date
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
    notification.alertAction = "be awesome!"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["CustomField1": "w00t"]
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

之前,我使用此代码从现在开始以时间间隔发送本地通知,但我希望能够将其设置为特定时间..

我希望在3:01发出通知,这是我的代码。 没有通知。

我错过了什么?

编辑:当打印出“日期”时,它正在打印:一个奇怪的日期,时间是提前四个小时。我的模拟器有正确的时间,我已经尝试过系统和默认时区。

我也尝试将我的测试设置在手动设置年/月/日的地方,然后设置4小时前的小时,这样它就给了我正确的时间,正确打印的时间但仍然没有当那个时间到来时发射通知。

0 个答案:

没有答案
相关问题