question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integrate HeadlessJs into Android

See original GitHub issue

Currently, 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 if RNBoundaryModule has a context and a resumed 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;

  1. 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
   }
  1. Figure out how to extend both JobIntentService and HeadlessJsTaskService, probably create a custom JobIntentHeadlessJsTaskService - has one of these been done already?
  2. Write the “GeofenceDataChanged” code in the JS layer to handle the events.
  3. 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jacek213commented, Jun 7, 2019

@eddieowens documenting on how to use it for dead/background app would be really useful.

0reactions
eddieowenscommented, May 19, 2019

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create an unstoppable service in React Native ...
How to create an unstoppable service in React Native using Headless JS (Android) · Step 1 - Creating the Bridge · Step 2...
Read more >
Headless JS
Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example,...
Read more >
React Native - Headless JS example
I'm new to native Android dev and I followed the docs, but they didn't work for me. Turns out I misunderstood and thought...
Read more >
Headless JS · React Native
Headless JS is a way to run tasks in JavaScript while your app is in the background. ... Following lines shows part of...
Read more >
Headless JS - React Native - W3cubDocs
Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found