location based push notification when app is not running?
Asked Answered
T

4

8

Is it possible when your app is not running but the user is still able to receive a push notifcation when he's near a particular location. This will require checking of the user's current latitude and longitude (even app is not running).

If it's possible, can you give some guidelines on how can I achieve it?

Tarrant answered 1/4, 2013 at 4:1 Comment(1)
Read "Starting the Significant-Change Location Service" in this document -> developer.apple.com/library/ios/#documentation/userexperience/…Megara
S
5

There are two methods you can make use of to track user location even when the app is not running:

  1. Significant-location change service.

    Call the method startMonitoringSignificantLocationChanges in a CLLocationManager instance. This will help significantly in saving the power but it does not provide with high precision compared to the second method below.

    You do not need to set the location key in UIBackgroundModes if you opt for this method.

  2. Standard location service.

    Call the method startUpdatingLocation in a CLLocationManager instance. This takes a lot of device power but it provides higher precision. Remember to set location key in UIBackgroundModes to make sure the tracking is still working even if the app is killed.

You can use either one of the methods above. I use the first method combined with Region Monitoring to trigger local notification.

You can take a look at the following Apple documentation for a more thorough explanations and examples:

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html

Samul answered 15/5, 2013 at 9:4 Comment(1)
Standard location service events will not relaunch the app, if it was killed. The standard location service delivers events normally while an app is running in the foreground. When your app is in the background, this service delivers events only when the location-updates background mode is enabled for the app. This service does not relaunch iOS apps that have been terminated. Taken from hereLeathaleather
W
3

Region(CLRegion) Based Notifications Even When the App is not Running at all

Well, this answer is not a quick one but Apple has introduced new concept in UILocalNotification. It is not required to send a push notification, iOS will automatically show a local notification when user enter/exits geographical area CLRegion. From iOS 8 and later, we can schedule a local notification based on location not by setting fireDate property.

    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 are more details from Apple.

Wriggler answered 3/2, 2016 at 8:17 Comment(1)
can we do silent notification so we can run the code?Ingest
R
0

You need to do 2 things:

  1. startMonitoringSignificantLocationChanges with the CLLocationManager. That's on the code side. You probably want to listen on the notification, and do whatever with the information that you're getting. But, if the app isn't running, you won't get this information unless you do two:
  2. In Capabilities: enable BackgroundModes, and Modes: Location updates. That way, I believe you'll always get notifications, even if your app slips off the inactive list.

However, the user can still turn it off (Settings->General->Background App Refresh).

Rapine answered 20/2, 2014 at 23:35 Comment(0)
B
0

You can do it when app is in background. firstly..call CLLocationManager deleagte whenever app goes to background. Whenever device will get a new latitude and longitude.. This deleagte will get fired. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations; Now call a webservice and update your current location. Then send a push notification.

Bramlett answered 19/11, 2014 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.