Wednesday, October 7, 2026
HomeiOS Developmentios - UNLocationNotificationTrigger don't set off

ios – UNLocationNotificationTrigger don’t set off

[ad_1]

Location notifications set off on simulator, tried in foreground, background and closed app state. It triggers in all instances on simulator, however on actual machine it doesn’t set off in any respect in any case, even foreground.

Right here is my code for location permission request

lazy var locationManager: CLLocationManager = createLocationManager()
    
    personal func createLocationManager() -> CLLocationManager {
      let supervisor = CLLocationManager()
      supervisor.allowsBackgroundLocationUpdates = true
      
      return supervisor
    }

init() {
        tremendous.init()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startMonitoringSignificantLocationChanges()
        locationManager.startUpdatingLocation()
        locationManager.allowsBackgroundLocationUpdates = true
}

func askForPermissionIfNeeded() {
        if CLLocationManager.locationServicesEnabled() {
            if !self.isLocationPermissionAsked {
                locationManager.requestAlwaysAuthorization() // additionally tried with requestWhenInUseAuthorization because the Apple documentation suggests for locationTriggered notifications
                locationManager.startUpdatingLocation()
            }
        }
    }

And right here is my code to request notification permissions

func requestNotificationAuthorization(completion: @escaping () -> Void) {
        let choices: UNAuthorizationOptions = [.sound, .alert, .badge]
        notificationCenter
            .requestAuthorization(choices: choices) { end result, _ in
                print("Notification Auth Request end result: (end result)")
                completion()
            }
    }

And right here is my code to register a location notification

personal func registerNotification(area: CLRegion) {
        let notificationContent = UNMutableNotificationContent()
        notificationContent.title = "Energetic location close by"
        notificationContent.physique = "Do you wish to bla bla bla?"
        notificationContent.sound = .default
        
        let set off = UNLocationNotificationTrigger(area: area, repeats: false)
        
        // timeTrigger used when monitoring area and at all times authorization
        // let timeTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)

        let request = UNNotificationRequest(
            identifier: area.identifier,
            content material: notificationContent,
            set off: set off)
        
        notificationCenter
            .add(request) { error in
                if error != nil {
                    print("Error: (String(describing: error))")
                }
            }
    }

All this works on simulator and would not on machine…or no less than no notification is triggered

BUT I discovered a workaround that no less than in my case works -> Area monitoring

I request at all times location permission and monitor what areas with locationManager like this

locationManager.startMonitoring(for: fenceRegion)

And with the usage of the delegate methodology didEnterRegion I register a notification there and substitute the set off with a time set off as an alternative of a location set off

func locationManager(_ supervisor: CLLocationManager, didEnterRegion area: CLRegion) {
        if area is CLCircularRegion {
            registerNotification(area: area)
// Change let timeTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) in registerNotification
        }
    }

I want to use the UNLocationNotificationTrigger the way in which they’re suposed to work and never have a workaround. Is it one thing I’m doing incorrect with Location Notifications?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments