【问题标题】:how to delay/debounce notification on timer (swift3)如何在计时器上延迟/去抖动通知(swift3)
【发布时间】:2017-06-05 15:37:14
【问题描述】:

如果按下开始按钮,我的通知将继续每 30 秒关闭一次。我想让它每次用户点击开始按钮时都会重置倒计时,并且通知只会在按下开始按钮后的 30 秒内出现。因此,如果用户在 30 秒结束前无限次点击开始按钮,用户将永远不会看到通知。

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

【问题讨论】:

    标签: ios timer swift3 notifications debouncing


    【解决方案1】:

    您只需要使计时器无效并重新安排它。试试这个。

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

    【讨论】:

    • 这行得通。我不敢相信事情就这么简单。似乎在编码中过度思考是常态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多