2017-06-05 54 views
1

如果按下開始按鈕,我的通知將繼續每隔30秒關閉一次。我想這樣做是爲了每當用戶點擊開始按鈕時,倒計時都會被重置,並且通知只會在按下開始按鈕後的30秒內出現。因此,如果用戶在30秒內無限次地點擊開始按鈕,用戶將永遠看不到通知。如何延遲/去抖通知定時器(swift3)

 import UIKit 
import UserNotifications 

class ViewController: UIViewController,UNUserNotificationCenterDelegate { 
var isGrantedAccess = false 
private var timer = Timer() 

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() 
} 

func timeString(time:TimeInterval) -> String { 

    let minutes = Int(time)/60 % 60 
    let seconds = Int(time) % 60 
    return String(format: " %02i : %02i", minutes, seconds) 
} 



override func viewDidLoad() { 
    super.viewDidLoad() 
    UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) { (granted, error) in 
      self.isGrantedAccess = granted 
    } 
    let stopAction = UNNotificationAction(identifier: "stop.action", title: "Stop", options: []) 
    let timerCategory = UNNotificationCategory(identifier: "timer.category", actions: [stopAction], intentIdentifiers: [], options: []) 
    UNUserNotificationCenter.current().setNotificationCategories([timerCategory]) 

} 


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    if response.actionIdentifier == "stop.action"{ 
     stopTimer() 
    } 
    completionHandler() 
} 

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    completionHandler([.alert,.sound]) 
}} 

回答

0

您只需要使計時器無效並重新計劃。嘗試這個。

func startTimer(){ 
    stopTimer() 
    let timeInterval = 30.0 
    if isGrantedAccess { //allowed notification and timer off 
     timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true, block: { (timer) in 
      self.sendNotification() 
     }) 
    } 
} 
+0

這工作。我不敢相信這很簡單。看起來在編碼過度的情況下它是常態 –