我正在尝试创建一个通知,该通知在使用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 次(我用地图和区域检查它作为地图中的圆圈覆盖)。但在那之后,所有其他尝试都失败了。
编辑: 对于那些阅读评论的人。它不再工作了。所以看起来很不协调。如果有人知道这是否是一个错误或知道如何修复它,我将非常感激。