iOS本地通知触发器处理

时间:2016-04-05 21:02:36

标签: ios swift uilocalnotification

我目前正在制作一款应用程序,用于计算步数和检查目标,如果遇到则发布本地通知。 我已经设置了本地通知,但我希望在那一刻只触发一次。我通过dispatch_once_t得到了这个工作:

if stepsData >= stepsGoalData {
            let localNotification = UILocalNotification()
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
            localNotification.fireDate = NSDate()
            localNotification.alertBody = "Acheived"
            localNotification.soundName = UILocalNotificationDefaultSoundName
    }

但是如果用户增加stepsGoalData,则当前代码不会触发通知。有人可以请我提出处理这个案子的想法。谢谢!

1 个答案:

答案 0 :(得分:1)

所以,你真的应该改变检查是否通知或不通知,以便它不仅考虑计数,而且还指示是否已经发出通知的标志。这可以是stepsGoalData旁边定义的简单Bool

现在,您的支票将是:

if stepsData >= stepsGoalData && !hasNotified {
    hasNotified = true
    ...

当您将stepsGoalData设置为新的目标值时,您还设置了hasNotified = false