ios - 后台通知

标签 ios swift background

在我的应用程序中,我需要在后台或屏幕锁定时更新位置。当用户接近某个指定位置(警报位置)时,应用程序会发出声音通知。当他们接近时,应用程序将发出一次蜂鸣声,下一次蜂鸣声两次,下一次蜂鸣声三声。

我知道如何在后台更新位置,但我在声音警报方面遇到困难。我将 Audio Session 与 AudioServicesPlaySystemSound

一起使用
do {
   let audioSession = AVAudioSession.sharedInstance()
   audioSession.setCategory(AVAudioSessionCategoryPlayback)
   try audioSession.setActive(true)
} catch {
   print("session does not init")
}
/* *** */
AudioServicesPlaySystemSound(beepSound)

我的算法如下: 当找到最近的警报位置时,应用程序会发出蜂鸣声。

它在前台工作,但在锁定屏幕下不起作用。

最佳答案

您需要在锁定屏幕下允许通知,为此您需要请求用户许可。在 AppDelegateapplication 函数中添加以下内容:

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/

相关文章:

ios - 弹出 collectionview 单元格并缩放 - uicollectionviewcell 动画

ios - 错误:在NSTimer选择器中,ARC无法将Objective-C指针隐式转换为'SEL _Nonnull'

ios - Swift 泛型和协议(protocol)

ios - 类型 'UIbutton' 的值不再有 'titleforState'

html - 每 10 秒更改一次正文背景

swift - 如何在 Swift 3.0 的 UITextField 中隐藏菜单中的粘贴选项?

ios - Swift 2.2、Firebase、JSQMessagesViewController - 无法将 [JSQMessage] 类型的返回表达式转换为 JSQMessageData 类型

ios - 无法使 : UICollectionElementKindCell 类型的 View 出队

html - 奇怪的 CSS 问题 - 背景属性在没有图像高度的情况下不起作用

html - 在手机上覆盖背景大小