2

I would like to show user 3 different local notifications as he is coming to exact point. So I set 3 circular regions with same center but different radiuses (500m, 1km, 2km). When I am closing to this point I get all 3 notifications at once. Why is it happening? Did I do something wrong with my code below or is it just function from Apple that shows more region notifications to consume less battery? Can I do this some other way (alert user when he is closing to some point?

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)")
        }
    }
}
Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182

1 Answers1

2

Make sure that when you're testing your app you consider the following:

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

Community
  • 1
  • 1