iOS 本地通知触发器处理

标签 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,目前代码不会触发通知。有人可以给我一个处理这个案子的想法吗?谢谢!

最佳答案

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

现在,您的支票将是:

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

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

关于iOS 本地通知触发器处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436986/

相关文章:

string - 如何快速检查字符串是否为零?

arrays - 如何在 UILocalNotification 中使用随机文本?

ios - 如何发送消息通知到连接的设备?

ios - 缓慢的 UI 更新,添加 Spinner

ios - 多线程上的核心数据操作导致插入但没有获取

ios - 在哪里快速设置委托(delegate)?

ios - 确定是否曾在 Swift 中调用过 `registerUserNotificationSettings`

ios - 在 Swift 3 中从 UserDefaults 读取存储数组时出现问题

ios - 如何通过单击 TableView 单元格打开新 View ?

ios - 隐藏和显示导航栏时导航栏后退按钮消失