iOS本地通知触发器处理



我目前正在制作一个应用程序,该应用程序可以计算所走的步数,并与目标进行检查,如果满足,则发布本地通知。我已经设置了本地通知,但我希望在那一刻只触发一次。我是通过dispatch_once_t:完成的

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

但如果用户增加了步骤GoalData,目前代码不会触发通知。有人能给我一个处理这个案子的主意吗。非常感谢。

因此,您实际上应该更改是否通知的检查,以便它不仅考虑计数,还考虑一个指示是否已发出通知的标志。这可以是在stepsGoalData旁边定义为简单Bool的var。

现在,你的支票是:

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

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

最新更新