Michel
Michel

Reputation: 11755

Notifications limited to a geographical area

It is something I only started to think about. But is there a way to send notifications to users of a given iOS app, only if they happen to be in a certain geographical area? Presuming I am the owner of the app and can modify the code to make that possibe (in case that is ever possibe)

More precisely the app has some kind of central service that sends notifications, but those notifications are only of interest to people in a given area. I do not want to bother the folks outside of that area with irrelevant notifications.

Upvotes: 0

Views: 64

Answers (2)

Arnab
Arnab

Reputation: 4356

For iOS 10 and later

If you want to send Local Notification with UNNotificationRequest, you can perform this as following:

  1. Ask for requestAlwaysAuthorization and add NSLocationAlwaysUsageDescription in info.plist file. Also add Background modes to Location.

    var locationManager:CLLocationManager
    

    inside viewWillAppear wirte this to ask for AlwaysAuthorization permission

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()
    
  2. Then, use either startMonitoringSignificantLocationChanges or startUpdatingLocation, to monitor location changes.

  3. Implement CLLocationManagerDelegate locationManager:didEnterRegion: and show local notification when enters into specific location.

    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion){
        //Show local notification when entered a desired location
        showLocalNotification("Entered \(region.identifier)")
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion){
        //Show local notification when exit from a desired location
         showLocalNotification("Exited \(region.identifier)")
    }
    

For earlier versions

If you want to use UILocalNotification, you can add CLRegion in UILocalNotification object. iOS will automatically show a local notification when user enter/exits geographical area CLRegion.

let localNotification = UILocalNotification()
localNotification.alertTitle = "Hi there"
localNotification.alertBody = "you are here"

let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 4.254, longitude: 88.25), radius: CLLocationDistance(100), identifier: "")
region.notifyOnEntry = true
region.notifyOnExit = false
localNotification.region = region       

localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.soundName = UILocalNotificationDefaultSoundName

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

Here is a link from Apple, about Getting the User’s Location

Upvotes: 1

Prakash
Prakash

Reputation: 872

If you are using local notification then you can use the geofence. for more info check this.

In geofence you can notify the on entry and on exit notifications. Based on the trigger you can set the notifications.

Upvotes: 0

Related Questions