Tomasz Szulc
Tomasz Szulc

Reputation: 4235

Local notification doesn't show up when hitting region

I'm working on simple To-Do app which uses IOS 8 feature of CLRegion support in UILocalNotification.

Here is the code I'm using to schedule local notification:

    var schedule = false
    let notification = UILocalNotification()

    /// Schedlue with date
    if let reminder = self.dateReminderInfo {
        schedule = true
        notification.fireDate = reminder.fireDate
        notification.repeatInterval = reminder.repeatInterval
        notification.alertBody = self.title
    }

    /// Schedule with location
    if let reminder = self.locationReminderInfo {
        schedule = true
        let region = CLCircularRegion(circularRegionWithCenter: reminder.place.coordinate, radius: CLLocationDistance(reminder.distance), identifier: taskObjectID)
        region.notifyOnEntry = reminder.onArrive
        region.notifyOnExit = reminder.onArrive
        notification.region = region
        notification.regionTriggersOnce = false
    }

    /// Schedule
    if schedule {
        notification.userInfo = ["objectID": taskObjectID]
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }

Scheduling with date works. I schedule notification, exit the app and at the proper time notification is displayed on the screen, great. The problem is when I'm scheduling notification with the location. I pass coordinates and radius in meters (e.g. 100 meters). The app isn't displaying nothing. I was testing it out of the home placing the point in distance of 1km and getting there. No notification displayed. I was playing also with simulator and changing location from available there to custom location near my place and back. No notification displayed. Where is the problem?

In the AppDelegate I'm registering for notifications:

    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

Here is the code of location services.

func startLocationService() {

    self.locationManager = CLLocationManager()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    let status = CLLocationManager.authorizationStatus()
    if status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.Denied {
        let title = (status == CLAuthorizationStatus.Denied) ? "Location services are off" : "Background location is not enabled"
        let message = "To use background location you must turn on 'Always' in the Location Services Settings"

        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

        /// Settings
        alertController.addAction(UIAlertAction.normalAction("Settings", handler: { _ in
            UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
            return
        }))

        /// Cancel
        alertController.addAction(UIAlertAction.cancelAction(String.cancelString(), handler: nil))

        self.presentViewController(alertController, animated: true, completion: nil)
    } else if status == CLAuthorizationStatus.NotDetermined {
        /// nothing
    }

    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.startUpdatingLocation()
}

CLLocationManager is working because I've got delegate method which is called very often.

/// Mark: CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    if let location = locations.first as? CLLocation {
        self.navigationItem.title = "\(location.coordinate.latitude)" + " " + "\(location.coordinate.longitude)"
        println(self.navigationItem.title)
    }
}

I'm trying second day to solve the problem and can't find good solution how to display local notification when user appear in the place or leave the place. I don't know if using UILocalNotification region property is good for this solution or maybe I should create mechanism which will search through the places I've got saved in the app and check every time location change. Any comments to this topic are appreciated too ;)

Thank you in advance.

Upvotes: 3

Views: 1200

Answers (2)

kbjeppesen
kbjeppesen

Reputation: 335

I am having the same issue, however I've found a way to trigger the notification. It looks like it is not responding to the radius parameter. The way I trigger the notification is by simulating the location being very far away from my region.

So if I set the region for a small city in Denmark, and move my location 1km, and back again, it does not trigger. However, if I set the region for the same small city in Denmark, and move my location to London and back again, then it will trigger.

So for me it looks like the radius parameter is somehow disgarded? I tried with radius on 250.0, 100.0, 10.0, 0.0001, and it had no impact at all.

Can this maybe inspire someone else to what the problem can be and how to solve it?

Upvotes: 0

Shrikar
Shrikar

Reputation: 870

I see that you are not registering for the notifications

let settings = UIUserNotificationSettings(forTypes: notificationType, categories: categories) application.registerUserNotificationSettings(settings)

You can find more information here iOS 8 Notifications in Swift and here CoreLocation and region Monitoring

Upvotes: 1

Related Questions