2017-06-05 6 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

これは機能しました。私はそれが簡単だとは信じられません。それは、過度の想起のコーディングでは、 –

関連する問題