后台任务完成后如何发送通知?

时间:2019-05-01 18:02:10

标签: ios swift timer notifications

我需要您的帮助!我想确保在达到最大计时器数时,即使在最小化模式下,我也会收到通知。我如何发出此通知? (在汇总模式下,计时器有效)

到目前为止,只有在打开的应用程序中的计时器结束时才会出现

// TimerFunc

func timerInstall(maxValue: CGFloat, cupBeforeStartIMG: String, cupAfterStartIMG: String, cupInProgressImg: String, cupInFinishImg: String ) {
    // Arguments
    let startTime = Date()
    timerCircle.maxValue = maxValue
    timerCircle.value = 0
    // Timer
        Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timerCounter) in
            self.timerActivateButton.isEnabled = false
            self.sortOfTeaPickerButton.isEnabled = false
            let TimePassed = Date().timeIntervalSince(startTime)
            self.timerCircle.value = CGFloat(TimePassed)
            let valueInt = Int(self.timerCircle.value)
            let valueMaxInt = Int(self.timerCircle.maxValue)
            self.leftTimeLabel.text = String("\(valueInt) секунд")
      // Switch
            switch self.timerCircle.value {
            case self.timerCircle.maxValue / 2:
                self.timerImage.image = UIImage(named: cupAfterStartIMG)
            case self.timerCircle.maxValue / 3:
                self.timerImage.image = UIImage(named: cupInProgressImg)
            case self.timerCircle.maxValue :
                self.timerImage.image = UIImage(named: cupInFinishImg)
                self.timerActivateButton.isEnabled = true
                self.sortOfTeaPickerButton.isEnabled = true
                timerCounter.invalidate()
                self.playerSound.play()
                self.timerActivateButton.backgroundColor = #colorLiteral(red: 0, green: 0.6549019608, blue: 0.462745098, alpha: 1)
                self.timerActivateButton.titleLabel?.text = "Новая чашка"
                self.leftTimeLabel.text = "Чай готов!"
                self.scheduleNotifications(inSeconds: 5)
            default:
                break
            }
        }
    }

// LocalNotification
func scheduleNotifications(inSeconds seconds : TimeInterval) {
    let date = Date(timeIntervalSinceNow: seconds)
    let notificationContent = UNMutableNotificationContent()
    notificationContent.body = "Ваш чай готов, доставайте пакетик!"
    notificationContent.sound = UNNotificationSound.default
    let calendar = Calendar.current
    let components = calendar.dateComponents([.month,.day,.hour,.minute,.second], from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
    let request = UNNotificationRequest(identifier: "YourTeaIsReady", content: notificationContent, trigger: trigger)
    let center = UNUserNotificationCenter.current()
    center.add(request, withCompletionHandler: nil)
}

在汇总模式下达到计时器的最大数量时,我想获得本地通知。

0 个答案:

没有答案