From 956d1cfff44c2f3b716fa3d337081b7dd76ed56b Mon Sep 17 00:00:00 2001 From: Marian Hello Date: Thu, 23 Aug 2018 11:38:26 +0200 Subject: [PATCH] Allow stopping location updates on status "285 Updates Not Required" Origin-PR: https://github.com/mauron85/react-native-background-geolocation/pull/271 Contributed-By: @danielgindi --- README.md | 32 ++++++++++++------- .../cordova/BackgroundGeolocationPlugin.java | 6 ++++ .../CDVBackgroundGeolocation.m | 6 ++++ www/BackgroundGeolocation.js | 3 +- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 52c7cd7fc..b4569ea2b 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,15 @@ function onDeviceReady() { BackgroundGeolocation.configure({ debug: false }); }); + BackgroundGeolocation.on('abort_requested', function() { + 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(function(status) { console.log('[INFO] BackgroundGeolocation service is running', status.isRunning); console.log('[INFO] BackgroundGeolocation services enabled', status.locationServicesEnabled); @@ -444,17 +453,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/CDVBackgroundGeolocation/src/main/java/com/tenforwardconsulting/bgloc/cordova/BackgroundGeolocationPlugin.java b/android/CDVBackgroundGeolocation/src/main/java/com/tenforwardconsulting/bgloc/cordova/BackgroundGeolocationPlugin.java index 9e07db64f..2a01a1b05 100644 --- a/android/CDVBackgroundGeolocation/src/main/java/com/tenforwardconsulting/bgloc/cordova/BackgroundGeolocationPlugin.java +++ b/android/CDVBackgroundGeolocation/src/main/java/com/tenforwardconsulting/bgloc/cordova/BackgroundGeolocationPlugin.java @@ -45,6 +45,7 @@ public class BackgroundGeolocationPlugin extends CordovaPlugin implements Plugin public static final String AUTHORIZATION_EVENT = "authorization"; 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 ACTION_START = "start"; public static final String ACTION_STOP = "stop"; @@ -564,6 +565,11 @@ public void onServiceStatusChanged(int status) { } } + @Override + public void onAbortRequested() { + sendEvent(ABORT_REQUESTED_EVENT, 0); + } + @Override public void onError(PluginException e) { sendError(e); diff --git a/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m b/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m index 57c827470..d2bbbc7c9 100644 --- a/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m +++ b/ios/CDVBackgroundGeolocation/CDVBackgroundGeolocation.m @@ -433,6 +433,12 @@ -(void) onAppPause:(NSNotification *)notification [facade switchMode:MAURBackgroundMode]; } +-(void) onAbortRequested +{ + NSLog(@"%@ %@", TAG, @"abort requested by the server"); + [self sendEvent:@"abort_requested"]; +} + /**@ * on UIApplicationDidFinishLaunchingNotification */ diff --git a/www/BackgroundGeolocation.js b/www/BackgroundGeolocation.js index 76d04e947..bb8e64d36 100644 --- a/www/BackgroundGeolocation.js +++ b/www/BackgroundGeolocation.js @@ -33,7 +33,8 @@ var BackgroundGeolocation = { 'error', 'authorization', 'foreground', - 'background' + 'background', + 'abort_requested' ], DISTANCE_FILTER_PROVIDER: 0,