My code has two buttons for starting and stoping the notification sequence. If you hit the start button the notification sequence begins. A notification will appear every 30 seconds for a infinite amount of time. What I want to do is have the notificiation appear 30 seconds after the start button is hit. So if the user hits the start button infinitely before the 30 seconds, (the countdown will reset at 30 every time) are up the notification will never appear. I have only included the code that is needed to solve this problem.
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()
}
@IBAction func startButton(_ sender: UIButton) {
startTimer()
}
@IBAction func stopButton(_ sender: UIButton) {
stopTimer()
}
Aucun commentaire:
Enregistrer un commentaire