UNLocation通知触发器未触发



我正在尝试创建一个通知,该通知在使用UNNotificationLocationTrigger进入区域时触发。我是这样实现的:

let destination = //destination is added by user through interface//
let notification = UNMutableNotificationContent()
notification.title = "You've reached your destination"
notification.body = "Some"
notification.sound = UNNotificationSound.default()
let destRegion = CLCircularRegion(center: destination, radius: 1000.0, identifier: "DistanceToDestination")
destRegion.notifyOnEntry = true
let trigger = UNLocationNotificationTrigger(region: destRegion, repeats: false)
let request = UNNotificationRequest(identifier: "destAlarm", content: notification, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
     if error == nil {
           print("Successful notification")
     } else {
           print(error ?? "Error")
     }
})

我在真实设备上尝试了一下,然后四处走动。当我进入该地区时,它完美地工作了 2 次(我用地图和区域检查了它作为地图中的圆形叠加)。但在那之后,所有其他尝试都失败了。

编辑:对于那些阅读评论的人。它不再工作了。所以它似乎很不一致。如果有人知道这是一个错误或知道如何修复它,我将不胜感激。

首先,你应该调用locationmanager.startupdatinglocation其次,您应该更改触发器,以便重复 = true

最新更新