iOS本地通知每天递增

时间:2016-09-08 12:17:33

标签: ios uilocalnotification

我创建了一个iOS应用,每天上午11:00发送本地通知。

我已关注this

我的代码是......

func setNotification(){

    let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var dateFire=NSDate()

    var fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())

    if (fireComponents.hour >= 11) {
        dateFire=dateFire.dateByAddingTimeInterval(86400)  // Use tomorrow's date

        fireComponents=calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())
    }


    fireComponents.hour = 11
    fireComponents.minute = 00

    dateFire = calendar.dateFromComponents(fireComponents)!

    let localNotification = UILocalNotification()
    localNotification.fireDate = dateFire
    localNotification.alertTitle = "Title"
    localNotification.alertBody = "Body"
    localNotification.repeatInterval = NSCalendarUnit.Day

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

}

现在应用程序运行良好,通知每天上午11:00发送,但问题是它们每天增加一个:(

昨天我收到了6封通知,今天我在上午11点收到了7封

请帮我解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

我试过这个和它的工作。您正在检查是否有条件并覆盖firecomponents以使其无用。

let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var dateFire=NSDate()

let fireComponents = calendar.components([.Month, .Day, .Hour, .Minute], fromDate:NSDate())

fireComponents.hour = 7
fireComponents.minute = 41
fireComponents.second = 00

dateFire = calendar.dateFromComponents(fireComponents)!

let localNotification = UILocalNotification()
localNotification.fireDate = dateFire
localNotification.alertBody = "Body"
localNotification.repeatInterval = NSCalendarUnit.Minute

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

有关详细信息https://wordpress.com/post/iosmygirlfriend.wordpress.com/48