【发布时间】: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,我已将其添加到问题的开头!