Integrate HeadlessJs into Android
See original GitHub issueCurrently, Android events will not trigger in the background if the app is in doze mode. In order to get around this, @BrantApps suggested the following
As far as I can tell, the
BoundaryEventBroadcastReceiver
is going to fire when we enter a geofence - all good there. With the code I wrote, these events would make it through to the JS layer ifRNBoundaryModule
has a context and aresumed
owning activity. For an activity to be resumed, it needs to have been pretty close to being “on-screen” in the recent past or close future if that makes sense.In native-native land (not RN), you would do your application work in the Job. I think the next evolution of this code for react-native before it can be merged would be to;
- In
BoundaryEventJobIntentService#sendEvent(String event, ArrayList<String> params)
refactor to start a HeadlessJS task with the name of the JS function to invoke with those parameters. Something like;private void sendEvent(String event, ArrayList<String> params) { final Bundle bundle = new Bundle(); bundle.putString("event", event); bundle.putStringArrayList("params", params); new HeadlessJsTaskConfig( "GeofenceDataChanged", // JS function to call bundle, // The data to send 5000, // Timeout for the task true); // Allowed in foreground }
- Figure out how to extend both
JobIntentService
andHeadlessJsTaskService
, probably create a customJobIntentHeadlessJsTaskService
- has one of these been done already?- Write the “GeofenceDataChanged” code in the JS layer to handle the events.
- Extend the library implementation to handle device restarts/location toggling. (Apparently on Android, the geofences are removed if the device reboots or the location functionality is toggled?)
I agree with this approach and think it’s the best way to ensure all events trigger in the background.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7 (3 by maintainers)
@eddieowens documenting on how to use it for dead/background app would be really useful.
I have just integrated the
HeadlessJSTask
into android and simplified the flow for geofence events (see pr #27).This is now in version
1.0.8
so if you want this fix, grab the latest. Thanks everyone!