iOS 本地通知触发器处理

iOS Local Notification Trigger Handling

我目前正在制作一个应用程序,该应用程序计算步行的步数并检查目标并在达到目标时发布本地通知。 我已经设置了本地通知,但我希望它在那一刻只触发一次。我通过 dispatch_once_t:

解决了这个问题
if stepsData >= stepsGoalData {
            let localNotification = UILocalNotification()
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
            localNotification.fireDate = NSDate()
            localNotification.alertBody = "Acheived"
            localNotification.soundName = UILocalNotificationDefaultSoundName
    }

但如果用户增加 stepsGoalData,目前代码不会触发通知。有人可以给我提供处理此案的想法吗?谢谢!

所以,您真的应该只更改是否通知的检查,这样它不仅会考虑计数,还会考虑一个标志来指示是否已发出通知。这可以是在 stepsGoalData 旁边定义的一个简单的 Bool.

现在,您的支票将是:

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

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