From c3c2369d34e19db7b24021efc7e4a115765036c1 Mon Sep 17 00:00:00 2001 From: Daniel Cohen Gindi Date: Tue, 21 Aug 2018 16:53:04 +0300 Subject: [PATCH] Allow stopping location updates on status "285 Updates Not Required" --- README.md | 32 ++++++++++++------- .../react/BackgroundGeolocationModule.java | 6 ++++ index.js | 3 +- .../RCTBackgroundGeolocation.m | 18 +++++++++-- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ff9505d6..5c6a8e30 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,15 @@ class BgTracking extends Component { console.log('[INFO] App is in foreground'); }); + BackgroundGeolocation.on('abort_requested', () => { + console.log('[INFO] Server responded with 285 Updates Not Required'); + + // Here we can decide whether we want stop the updates or not. + // If you've configured the server to return 285, then it means the server does not require further update. + // So the normal thing to do here would be to `BackgroundGeolocation.stop()`. + // But you might be counting on it to receive location updates in the UI, so you could just reconfigure and set `url` to null. + }); + BackgroundGeolocation.checkStatus(status => { console.log('[INFO] BackgroundGeolocation service is running', status.isRunning); console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled); @@ -545,17 +554,18 @@ Unregister all event listeners for given event ## Events -| Name | Callback param | Platform | Provider* | Description | -|---------------------|------------------------|--------------|-------------|----------------------------------------| -| `location` | `Location` | all | all | on location update | -| `stationary` | `Location` | all | DIS,ACT | on device entered stationary mode | -| `activity` | `Activity` | Android | ACT | on activity detection | -| `error` | `{ code, message }` | all | all | on plugin error | -| `authorization` | `status` | all | all | on user toggle location service | -| `start` | | all | all | geolocation has been started | -| `stop` | | all | all | geolocation has been stopped | -| `foreground` | | Android | all | app entered foreground state (visible) | -| `background` | | Android | all | app entered background state | +| Name | Callback param | Platform | Provider* | Description | +|---------------------|------------------------|--------------|-------------|--------------------------------------------------| +| `location` | `Location` | all | all | on location update | +| `stationary` | `Location` | all | DIS,ACT | on device entered stationary mode | +| `activity` | `Activity` | Android | ACT | on activity detection | +| `error` | `{ code, message }` | all | all | on plugin error | +| `authorization` | `status` | all | all | on user toggle location service | +| `start` | | all | all | geolocation has been started | +| `stop` | | all | all | geolocation has been stopped | +| `foreground` | | Android | all | app entered foreground state (visible) | +| `background` | | Android | all | app entered background state | +| `abort_requested` | | all | all | server responded with "285 Updates Not Required" | ### Location event | Location parameter | Type | Description | diff --git a/android/lib/src/main/java/com/marianhello/bgloc/react/BackgroundGeolocationModule.java b/android/lib/src/main/java/com/marianhello/bgloc/react/BackgroundGeolocationModule.java index 8920053a..14a0649f 100644 --- a/android/lib/src/main/java/com/marianhello/bgloc/react/BackgroundGeolocationModule.java +++ b/android/lib/src/main/java/com/marianhello/bgloc/react/BackgroundGeolocationModule.java @@ -39,6 +39,7 @@ public class BackgroundGeolocationModule extends ReactContextBaseJavaModule impl public static final String START_EVENT = "start"; public static final String STOP_EVENT = "stop"; + public static final String ABORT_REQUESTED_EVENT = "abort_requested"; public static final String ERROR_EVENT = "error"; private static final int PERMISSIONS_REQUEST_CODE = 1; @@ -406,4 +407,9 @@ public void onServiceStatusChanged(int status) { public void onError(PluginException error) { sendError(error); } + + @Override + public void onAbortRequested() { + sendEvent(ABORT_REQUESTED_EVENT, null); + } } diff --git a/index.js b/index.js index 62dd2f2e..aa0d8d30 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,8 @@ var BackgroundGeolocation = { 'error', 'authorization', 'foreground', - 'background' + 'background', + 'abort_requested' ], DISTANCE_FILTER_PROVIDER: 0, diff --git a/ios/RCTBackgroundGeolocation/RCTBackgroundGeolocation.m b/ios/RCTBackgroundGeolocation/RCTBackgroundGeolocation.m index 43578c48..bbb5cdfd 100755 --- a/ios/RCTBackgroundGeolocation/RCTBackgroundGeolocation.m +++ b/ios/RCTBackgroundGeolocation/RCTBackgroundGeolocation.m @@ -359,20 +359,34 @@ - (void) onActivityChanged:(MAURActivity *)activity [self sendEvent:@"activity" resultAsDictionary:[activity toDictionary]]; } --(void) onAppResume:(NSNotification *)notification +- (void) onAppResume:(NSNotification *)notification { RCTLogInfo(@"RCTBackgroundGeoLocation resumed"); [facade switchMode:MAURForegroundMode]; [self sendEvent:@"foreground"]; } --(void) onAppPause:(NSNotification *)notification +- (void) onAppPause:(NSNotification *)notification { RCTLogInfo(@"RCTBackgroundGeoLocation paused"); [facade switchMode:MAURBackgroundMode]; [self sendEvent:@"background"]; } +- (void) onAbortRequested +{ + RCTLogInfo(@"RCTBackgroundGeoLocation abort requested by the server"); + + if (_bridge) + { + [self sendEvent:@"abort_requested"]; + } + else + { + [facade stop:nil]; + } +} + /**@ * on UIApplicationDidFinishLaunchingNotification */