我目前正在制作一个应用程序,它可以计算步行的步数并检查目标并在达到目标时发布本地通知。 我已经设置了本地通知,但我希望该通知在那一刻只触发一次。我通过 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
的 var。
现在,您的支票将是:
if stepsData >= stepsGoalData && !hasNotified {
hasNotified = true
...
当您将 stepsGoalData
设置为新的目标值时,您还设置了 hasNotified = false
。
关于iOS 本地通知触发器处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436986/