Skip to content

Commit

Permalink
Allow stopping location updates on status "285 Updates Not Required" (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi authored and mauron85 committed Aug 23, 2018
1 parent b20a6ec commit e5814e2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
32 changes: 21 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -546,17 +555,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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var BackgroundGeolocation = {
'error',
'authorization',
'foreground',
'background'
'background',
'abort_requested'
],

DISTANCE_FILTER_PROVIDER: 0,
Expand Down
18 changes: 16 additions & 2 deletions ios/RCTBackgroundGeolocation/RCTBackgroundGeolocation.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit e5814e2

Please sign in to comment.