如果关闭应用程序而不发送通知,如何在计时器上播放声音/振动?

时间:2016-05-13 15:46:47

标签: swift timer background notifications

我正在我的应用程序中构建锻炼计时器。工作流程是:

  1. 用户点击“开始计时器”按钮
  2. 计时器倒计时为90 秒
  3. 计时器在90秒后结束并触发PlayAlertSound 振动
  4. 这仅在应用程序打开时才有效,我不希望我的用户在计时器达到0时查看我的应用程序。我可以发送通知,但之后我会在课程中发送数十个通知一次锻炼。就个人而言,我不喜欢从单个应用程序收到大量通知。感觉很吵。

    有没有办法让应用程序在关闭应用程序时发送振动而不发送通知?

    我试图询问后台资源,所以我的计时器在关闭应用程序后运行,但即使计时器继续运行,它也不会触发振动,直到我打开应用程序,即用户需要查看他们的电话。

    这是我的代码:

    class TimerViewController: UIViewController {
    
    @IBOutlet weak var startTimerButton: UIButton!
    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var resetTimerButton: UIButton!
    
    var timer = NSTimer() 
    let timeInterval:NSTimeInterval = 0.05
    let timerEnd:NSTimeInterval = 90
    var timeCount:NSTimeInterval = 0.0
    
    var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func startTimerButtonTapped(sender: UIButton) {
        backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
            UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!)
        })
    
        if !timer.valid { 
            timerLabel.text = timeString(timeCount)
            timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval,
                                                           target: self,
                                                           selector: #selector(TimerViewController.timerDidEnd(_:)),
                                                           userInfo: "Time is up!!",
                                                           repeats: true) //repeating timer in the second iteration
        }
    
    }
    
    @IBAction func resetTimerButtonTapped(sender: UIButton) {
        timer.invalidate()
        resetTimeCount()
        timerLabel.text = timeString(timeCount)
    }
    
    func resetTimeCount(){
        timeCount = timerEnd
    }
    
    func timeString(time:NSTimeInterval) -> String {
        let minutes = Int(time) / 60
        //let seconds = Int(time) % 60
        let seconds = time - Double(minutes) * 60
        let secondsFraction = seconds - Double(Int(seconds))
        return String(format:"%02i:%02i.%02i",minutes,Int(seconds),Int(secondsFraction * 100.0))
    }
    
    func timerDidEnd(timer:NSTimer){
        //timerLabel.text = timer.userInfo as? String
        //timer that counts down
        timeCount = timeCount - timeInterval
        if timeCount <= 0 {  
            timerLabel.text = "Time is up!!"
            timer.invalidate()
            AudioServicesPlayAlertSound(kSystemSoundID_Vibrate)
            //pushNotification()
        } else { //update the time on the clock if not reached
            timerLabel.text = timeString(timeCount)
        }
    
    }
    //    
    //    func pushNotification() {
    //        let notification = UILocalNotification()
    //        notification.alertAction = "Go back to App"
    //        notification.alertBody = "This is a Notification!"
    //        notification.fireDate = NSDate(timeIntervalSinceNow: 1)
    //        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    //    }
    //    
    
    }
    

1 个答案:

答案 0 :(得分:0)

不幸的是,当收到本地通知时,你无法唤醒你的应用程序 - 这是一件好事,否则会被滥用很多。

但你可以像这样在后台播放音频: How to Play Audio in Background Swift?

尝试播放90秒的静音,然后播放闹铃声。