【问题标题】:continuously setting new local notification - ios不断设置新的本地通知 - ios
【发布时间】:2017-12-07 04:19:25
【问题描述】:

我正在模拟警报。

为了做到这一点,我尝试在某些条件为真时每 X 秒发送一个新的本地通知,并在某些条件为假时停止(当用户取消警报时)

我有一个Set 按钮和一个Stop 按钮。

以下代码用于按下Set 按钮时:

@IBAction func setNotification(_ sender: Any) {
//        timeEntered = time.text!
//        let hrMin = timeEntered.components(separatedBy: ":");
        let content = UNMutableNotificationContent()
        content.title = "How many days are there in one year"
        content.subtitle = "Do you know?"
        content.body = "Do you really know?"
        content.badge = 1
        content.sound = UNNotificationSound(named: "alarm.aiff");

        while (repeatNotifications) {
            trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false);

            let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

            UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
        }
}

Stop 按钮被按下时:

@IBAction func stopRepeat(_ sender: Any) {
    repeatNotifications = false
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}

现在,当我按下Set 按钮时,它卡在按下模式(颜色发生变化),除了主页按钮之外,我无法按下其他任何东西(即:Stop 按钮)。此外,没有设置本地通知。

有什么想法可以做到这一点吗?

tl;dr:如何在按下某个按钮之前每 X 秒设置一次新的本地通知

【问题讨论】:

  • 您可能不得不提及您想通过此功能实现什么目标?您是否正在尝试开发警报应用程序?
  • @MDAslamAnsari,我已将其添加到问题的开头!

标签: ios uilocalnotification


【解决方案1】:

您的 while 条件运行得比您想象的要快。它可能会通过创建对象数量来阻塞您的通知类。请使用计时器每 5 秒运行一次以推送通知。 您可以使用Instruments Tool查看问题。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
相关资源
最近更新 更多