我想向用户显示 3 个不同的本地通知,因为他要准确。所以我设置了3个中心相同但半径不同的圆形区域(500m,1km,2km)。当我接近这一点时,我会一次收到所有 3 个通知。为什么会这样?我在下面的代码中是否做错了什么,或者它只是来自 Apple 的功能,显示更多区域通知以消耗更少的电池?我可以通过其他方式做到这一点(当他接近某个点时提醒用户?
func createLocalNotification(id: String, title: String, body: String, center: CLLocationCoordinate2D, radius: CLLocationDistance, repeats: Bool) {
let notificationCenter = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default()
let region = CLCircularRegion(center: center, radius: radius, identifier: id)
region.notifyOnEntry = true
region.notifyOnExit = false
let trigger = UNLocationNotificationTrigger(region: region, repeats: repeats)
let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
notificationCenter.add(request) { (error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}