gpt4 book ai didi

ios - 一次触发具有不同半径 CLCircularRegion 的基于位置的通知

转载 作者:可可西里 更新时间:2023-11-01 01:58:42 25 4
gpt4 key购买 nike

我想向用户显示 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)")
}
}
}

最佳答案

确保在测试应用时考虑以下因素:

When testing your region monitoring code in iOS Simulator or on a device, realize that region events may not happen immediately after a region boundary is crossed. To prevent spurious notifications, iOS doesn’t deliver region notifications until certain threshold conditions are met. Specifically, the user’s location must cross the region boundary, move away from the boundary by a minimum distance, and remain at that minimum distance for at least 20 seconds before the notifications are reported.

The specific threshold distances are determined by the hardware and the location technologies that are currently available. For example, if Wi-Fi is disabled, region monitoring is significantly less accurate. However, for testing purposes, you can assume that the minimum distance is approximately 200 meters.

Documentation

关于ios - 一次触发具有不同半径 CLCircularRegion 的基于位置的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48443944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com