0

I have an app which is set to monitor entry into regions. On entry, the app will alert the user with a local notification.

This works fine when the app is on, or in the background. However if the app is terminated, the region monitoring never raises the local notification.

I have set my "background modes" key in the info.plist.

Could this be because my CLLocation code is not in the AppDelegate (instead it is in a singleton)?

Could this be because it's not possible to run code to raise the location notification from a terminated state?

Here's my code when the region is entered:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {

    UIApplication* app = [UIApplication sharedApplication];
    UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];

    notifyAlarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];;
    notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
    notifyAlarm.repeatInterval =NSDayCalendarUnit;
    notifyAlarm.alertBody = [Installation currentInstallation].reminderText;

    [app scheduleLocalNotification:notifyAlarm];
}
theDuncs
  • 4,649
  • 4
  • 39
  • 63
  • Can you please check https://stackoverflow.com/questions/47569430/getting-the-users-location-when-app-is-terminated/52921038#52921038 ? – Hiren Oct 22 '18 at 00:50

1 Answers1

0

There are 2 things happen: a) If the application is suspended when an update occurs, the system wakes it up in the background to handle the update.

b) If the application starts this service and is then terminated, the system relaunches the application automatically when a new location becomes available.

What we can now do is turn on significant location updates when the user hits the home key and we can let the system wake us up when needed.

-(void) applicationDidEnterBackground:(UIApplication *) application
{

    // You will also want to check if the user would like background location
    // tracking and check that you are on a device that supports this feature.
    // Also you will want to see if location services are enabled at all.
    // All this code is stripped back to the bare bones to show the structure
    // of what is needed.

    [locationManager startMonitoringSignificantLocationChanges];
}

Then to perhaps switch to higher accuracy when the application is started up, use;

-(void) applicationDidBecomeActive:(UIApplication *) application
{

       [locationManager stopMonitoringSignificantLocationChanges];
       [locationManager startUpdatingLocation];
}

Next you’ll likely want to change your location manager delegate to handle background location updates.

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{

    BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }

    // Handle location updates as normal.

    if (isInBackground)
    {
        // Do, if you have to send location to server, or what you need
    }
    else
    {
        // ...
    }
}

Please Note: Location monitoring in background will take effect on battery usage.

This code is taken from http://www.mindsizzlers.com/2011/07/ios-background-location/

Soumalya Banerjee
  • 1,966
  • 3
  • 22
  • 28