24

Is it possible to make use of geo-based push notifications on iOS when the application is killed (not in the background)?

I am interested in building an app, where the user will choose a position on a map, and then if he/she for example is close to that area a local geo-based push notification would be triggered.

However is this "idea" even possible? Can the GPS run and compare coordinates when the app is killed and run and notify the user when is in place? Is there a tutorial/article/more information of any kind on the subject that i could read?

Most of the information I read online were more like general ideas of implementing without anything specific though on the matter.

3
  • Why you don't want to run application in background (means after killing app)? I applied the same concept in my app with background running feature. Can you elaborate more . Mar 23, 2013 at 19:57
  • for example. Lets say the user makes a reminder and he wants to see it when he goes to his grandmas house. This could be next week. for sure the phone might be reseted or he could have deleted the app from the background. So i need the push to be trigered even when the app is not running in the background. Can you tell me the name of the app i am really interested to see what you have implemented :) Mar 23, 2013 at 20:38
  • 1. GPS is not running when application killed so not possible. 2. As you comment an example, from CMS side you can set push notification on reminder time. On web service you can design a algorithm such that sends push notification as you set via mobile reminder. Mar 24, 2013 at 7:03

1 Answer 1

34
+150

For tracking a user's location while the app is not running (ie. has previously been terminated), there are two options:

  1. From the iOS app programming guide under "Tracking the User's Location":

    The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.

    However, according to the CLLocationManager class reference, it's not too accurate and updates are infrequent:

    Note: Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

  2. Region Monitoring works in a similar way - including restarting the app after being terminated - but with higher accuracy (depending on availability of Wifi networks and cell towers):

    The specific threshold distances are determined by the hardware and the location technologies that are currently available. For example, if Wi-Fi is disabled, region monitoring is significantly less accurate. However, for testing purposes, you can assume that the minimum distance is approximately 200 meters.

    Another region monitoring consideration is that (according to the CLLocationManager class reference) region entry and exit notifications might only be received 3-5 minutes or so after crossing the region's boundaries.

    Depending on the actual requirements, region monitoring could be used for obtaining a "rough" location and then when the user is within a specific region, start up the more accurate GPS based service on the location manager. When the user leaves the region of interest, turn off the GPS service to preserve battery and revert to the rough location monitoring service (ie. region monitoring) once again. Here's a basic implementation:

    SomeViewController.m:

    ...
    @interface SomeViewController () <CLLocationManagerDelegate>
    
    @property (nonatomic, strong) CLLocationManager *locationManager;
    @property (nonatomic, strong) CLRegion *someRegion;
    
    @end
    
    @implementation SomeViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.locationManager = [[CLLocationManager alloc] init];
    
        CLLocationDistance radius = 10; // 10 metre sensitivity
        self.someRegion = [[CLRegion alloc] initCircularRegionWithCenter:someCoordinates radius:radius identifier:@"Smithtown Dry Cleaners"];
    
        self.locationManager.delegate = self;
        [self.locationManager startMonitoringForRegion:self.someRegion];
    
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = 10;
        [self.locationManager startUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
    {
        [self.locationManager startUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
    {
        [self.locationManager stopUpdatingLocation];
    }
    
    // Delegate method from the CLLocationManagerDelegate protocol.
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        CLLocation* location = [locations lastObject];
    
        // If the user's current location is not within the region anymore, stop updating
        if ([self.someRegion containsCoordinate:location.coordinate] == NO) {
            [self.locationManager stopUpdatingLocation];
        }
    
        NSString *locationData = [NSString stringWithFormat:@"latitude %+.6f, longitude %+.6f\n",
                                  location.coordinate.latitude,
                                  location.coordinate.longitude];
        NSLog(@"%@", locationData);
    
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = locationData;
        localNotification.alertAction = @"Location data received";
        localNotification.hasAction = YES;
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }
    

    Remember to add the appropriate entries to the application's plist file so the app will run in the background with access to the appropriate resources:

    MyApp-Info.plist:

    <key>UIBackgroundModes</key>
    <array>
            ...
            <string>location</string>
    </array>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
            ...
            <string>location-services</string>
            <string>gps</string>
    </array>
    

    The above code assumes the use of iOS6 and ARC

12
  • Yes it certainly looks so! Do you have any idea though what precision we are talking about in this case? Cause i have seen some apps(didnt download them cause they were expensive) that would support geo-push but with precision of about 500m of the area. This would be too much for my purpose. I dont know if you could select a precision of around 50-100m and how much battery would it cost to run it all the time though. I look more into it now Mar 23, 2013 at 22:21
  • Since the significant-change location service relies on the device changing cell towers, it might not be good enough.
    – gavdotnet
    Mar 23, 2013 at 22:32
  • However, it should be possible to listen for the "significant change" notification that wakes the app up (if terminated) and then start using the standard background location services - which provides better accuracy - until the app is terminated again. You will of course lose some accuracy when the app is not running, but the rest of the time it would be as accurate as any backgrounded app gets.
    – gavdotnet
    Mar 23, 2013 at 22:45
  • 1
    @ArpitKumar: If the app had started "region monitoring" or "significant location change monitoring" before it was terminated, the app will be restarted in the background (when the location changes sufficiently) and delegate method locationManager:didUpdateLocations: will be called.
    – gavdotnet
    Mar 25, 2013 at 20:43
  • 1
    @donparalias: Yes and no. App needs to be running (foreground or background) to make use GPS. BUT... iOS can determine location without GPS and "unterminate" an app into background mode when at a specific location/region (as demonstrated in answer). From the background you can turn on the GPS and check more accurate location information as well as send local push notifications.
    – gavdotnet
    Mar 25, 2013 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.