gpt4 book ai didi

ios - 如何在 iOS 中安排可以暂停或删除的本地通知?

转载 作者:行者123 更新时间:2023-11-30 13:11:44 25 4
gpt4 key购买 nike

我正在尝试使用以下 iOS 库实现本地前台通知:https://github.com/kunass2/BSForegroundNotification

我希望在倒计时达到 0 后触发通知,但无法找到一种好方法来实现计时器上的暂停和重新启动按钮,同时暂停通知或设置新通知。

这是我当前的实现,它会失败,因为如果我按重新启动打算为重置计时器启用新通知的触发,旧通知也会被触发:

func setupLocalNotifications() { // called whenever current timer countdown reaches 0
let notification = BSForegroundNotification(userInfo: userInfoForCategory("ONE_BUTTON"))
notification.timeToDismissNotification = NSTimeInterval(10)
// Delay 10 seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(self.timeRemaining * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
if self.willDisplayForegroundNotification {
notification.presentNotification()
}
}
notification.delegate = self
}

@IBAction func startPauseButtonPressed(sender: AnyObject) {
if self.counting {
self.timer?.invalidate()
UIApplication.sharedApplication().cancelAllLocalNotifications()
self.willDisplayForegroundNotification = false
self.counting = false
startPauseButton.setTitle("Resume", forState: .Normal)
startPauseButton.setImage(UIImage(named: "Play.png"), forState: .Normal)
}
else {
setupLocalNotifications()
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(NewFocusViewController.countdown), userInfo: nil, repeats: true)
self.willDisplayForegroundNotification = true
self.counting = true
startPauseButton.setTitle("Pause", forState: .Normal)
}
}

@IBAction func restartButtonPressed(sender: AnyObject) {
self.timer?.invalidate()
UIApplication.sharedApplication().cancelAllLocalNotifications()
self.timeRemaining = Double(self.timeMax)
self.counting = false
self.willDisplayForegroundNotification = false
self.updateTimer()
self.startPauseButton.setTitle("Start", forState: .Normal)
}

最佳答案

我通过在 App Delegate 中实现 didReceiveLocalNotification() 方法来呈现前台通知,解决了这个问题。

关于ios - 如何在 iOS 中安排可以暂停或删除的本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38647802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com