繁体   English   中英

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的var。

现在,您的支票将是:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM