Skip to content

Commit c86b313

Browse files
committed
End background task after receiving location
The background task does not need to be running for the significant location change monitoring that allows background location tracking.
1 parent 30e2622 commit c86b313

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignalLocation.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,19 @@ + (void)showLocationSettingsAlertController {
290290
}];
291291
}
292292

293+
+ (BOOL)backgroundTaskIsActive {
294+
return fcTask && (fcTask != UIBackgroundTaskInvalid);
295+
}
296+
293297
+ (void)requestLocation {
294298
onesignal_Log(ONE_S_LL_DEBUG, @"OneSignalLocation Requesting Updated Location");
295299
id clLocationManagerClass = NSClassFromString(@"CLLocationManager");
296300
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground
297301
&& [clLocationManagerClass performSelector:@selector(significantLocationChangeMonitoringAvailable)]) {
298302
[locationManager performSelector:@selector(startMonitoringSignificantLocationChanges)];
303+
if ([self backgroundTaskIsActive]) {
304+
[self endTask];
305+
}
299306
} else {
300307
[locationManager performSelector:@selector(requestLocation)];
301308
}
@@ -345,11 +352,17 @@ - (void)locationManager:(id)manager didUpdateLocations:(NSArray *)locations {
345352
[OneSignalLocation sendLocation];
346353

347354
[OneSignalLocation sendAndClearLocationListener:PERMISSION_GRANTED];
355+
if ([OneSignalLocation backgroundTaskIsActive]) {
356+
[OneSignalLocation endTask];
357+
}
348358
}
349359

350360
- (void)locationManager:(id)manager didFailWithError:(NSError *)error {
351361
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"CLLocationManager did fail with error: %@", error]];
352362
[OneSignalLocation sendAndClearLocationListener:ERROR];
363+
if ([OneSignalLocation backgroundTaskIsActive]) {
364+
[OneSignalLocation endTask];
365+
}
353366
}
354367

355368
+ (void)resetSendTimer {

0 commit comments

Comments
 (0)