Reputation: 11755
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
Reputation: 4356
If you want to send Local Notification with UNNotificationRequest
, you can perform this as following:
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()
Then, use either startMonitoringSignificantLocationChanges
or startUpdatingLocation
, to monitor location changes.
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)")
}
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