If the start button is pressed my notification will go continue to go off every 30 seconds infinitely. I want to make it so that every time the user hits the start button the countdown is reset and the notification will only appear in the 30 seconds after the start button is pressed. So if the user hits the start button before the 30 seconds are up for a infinite amount of times the user will never see the notification.
func startTimer(){
let timeInterval = 30.0
if isGrantedAccess && !timer.isValid { //allowed notification and timer off
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true, block: { (timer) in
self.sendNotification()
})}}
func stopTimer(){
//shut down timer
timer.invalidate()
//clear out any pending and delivered notifications
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
func sendNotification(){
if isGrantedAccess{
let content = UNMutableNotificationContent()
content.title = "HIIT Timer"
content.body = "30 Seconds Elapsed"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "timer.category"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.001, repeats: false)
let request = UNNotificationRequest(identifier: "timer.request", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error{
print("Error posting notification:\(error.localizedDescription)")
}}}}
@IBAction func startButton(_ sender: UIButton) {
startTimer()
}