gpt4 book ai didi

ios - 如何保持 UNUserNotificationCenter 声音播放直到用户停止它

转载 作者:行者123 更新时间:2023-11-28 17:31:12 30 4
gpt4 key购买 nike

有几篇文章解决了同样的问题,但(据我所见)它们都是 4 年前的,而且是在 Objective-C 中。我正在使用 Swift 4.2。

我正在制作一个倒计时应用程序。当应用程序在后台并且计时器到期时,我希望通知声音继续播放,直到用户通过点击通知来停止它。

到目前为止,我只从后台/锁屏播放一次警报声。

这是我正在使用的方法。

func notifications(title: String, message: String){

center.delegate = self

let restartAction = UNNotificationAction(identifier: "RESTART_ACTION", title: "Restart timer", options: UNNotificationActionOptions(rawValue: 0))

let stopAction = UNNotificationAction(identifier: "STOP_ACTION", title: "Stop", options: UNNotificationActionOptions(rawValue: 0))

let expiredCategory = UNNotificationCategory(identifier: "TIMER_EXPIRED", actions: [restartAction, stopAction], intentIdentifiers: [], options: UNNotificationCategoryOptions(rawValue: 0))

// Register the notification categories.
center.setNotificationCategories([expiredCategory])

let content = UNMutableNotificationContent()
content.title = title
content.body = message
content.categoryIdentifier = "TIMER_EXPIRED"
content.sound = UNNotificationSound(named:UNNotificationSoundName(rawValue: _currentTimer.getSoundEffect+".mp3"))

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(_currentTimer.endTime), repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

center.add(request) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}

最佳答案

因此,对于这个问题,我要确定的答案是没有答案。 Apple 不希望第三方开发者在任何情况下播放通知声音超过 30 秒,即使是构建一个定时器应用程序。他们在时钟应用程序中的计时器将关闭,直到您将其静音,但不允许第三方开发人员这样做。

关于ios - 如何保持 UNUserNotificationCenter 声音播放直到用户停止它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52459408/

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