在我的应用程序中,我需要在后台或屏幕锁定时更新位置。当用户接近某个指定位置(警报位置)时,应用程序会发出声音通知。当他们接近时,应用程序将发出一次蜂鸣声,下一次蜂鸣声两次,下一次蜂鸣声三声。
我知道如何在后台更新位置,但我在声音警报方面遇到困难。我将 Audio Session 与 AudioServicesPlaySystemSound
do {
let audioSession = AVAudioSession.sharedInstance()
audioSession.setCategory(AVAudioSessionCategoryPlayback)
try audioSession.setActive(true)
} catch {
print("session does not init")
}
/* *** */
AudioServicesPlaySystemSound(beepSound)
我的算法如下: 当找到最近的警报位置时,应用程序会发出蜂鸣声。
它在前台工作,但在锁定屏幕下不起作用。
最佳答案
您需要在锁定屏幕下允许通知,为此您需要请求用户许可。在 AppDelegate
的 application
函数中添加以下内容:
let notificationCategory = UIMutableUserNotificationCategory()
let categories = Set<UIUserNotificationCategory>(arrayLiteral: notificationCategory)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)
application.registerUserNotificationSettings(settings)
关于ios - 后台通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37454124/