重复推送警报Swift

时间:2015-02-19 14:27:16

标签: ios swift push-notification nsdate

嗨,我正试图在当地时间每天早上7点(不是格林尼治标准时间)快速发出推送警报。

这是我的代码:

    func scheduleLocalNotification() {

        var localNotification = UILocalNotification()
        localNotification.timeZone = timeZone
        //localNotification.fireDate = Here is where I need help.
        localNotification.alertBody = "FooBar"
        localNotification.alertAction = "BarFoo"

    }

我似乎无法弄清楚如何实现每天早上7点在当地时间发送推送通知的代码,而不是仅仅在上午7点发送一次。我该怎么做呢?是否有可以执行此操作的NSDate对象?不同的代码在一起?

谢谢!

1 个答案:

答案 0 :(得分:1)

使用repeatInterval字段设置重复并使用NSCalendar计算下一个上午7点

let calendar = NSCalendar.currentCalendar()

// Calculate the next 7 AM
var date = calendar.dateBySettingHour(7, minute: 0, second: 0, ofDate: NSDate(), options:nil)
if date?.timeIntervalSinceNow < 0 {
    date = calendar.dateByAddingUnit(.CalendarUnitDay, value: 1, toDate: date!, options: nil)
}
localNotification.fireDate = date

// Set up a daily repeat
localNotification.repeatInterval = .CalendarUnitDay
localNotification.repeatCalendar = calendar