Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow stopping location updates on status "285 Updates Not Required" (Closes #270) #271

Merged
merged 1 commit into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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 |
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