2

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

1 回答 1

2

确保在测试应用时考虑以下事项:

在 iOS 模拟器或设备上测试您的区域监控代码时,请意识到区域事件可能不会在跨越区域边界后立即发生。为了防止虚假通知,iOS 在满足某些阈值条件之前不会发送区域通知。具体来说,用户的位置必须跨越区域边界,离开边界最小距离,并在报告通知之前保持在该最小距离至少 20 秒。

具体的阈值距离由硬件和当前可用的定位技术决定。例如,如果禁用 Wi-Fi,则区域监控的准确性会大大降低。但是,出于测试目的,您可以假设最小距离约为 200 米。

文档

于 2018-01-26T13:58:00.890 回答