Detached ExpoKit and Headless JS

#1

Hi there,

I have been having problems trying to get Headless JS task to execute on a detached ExpoKit project (SDK25). I have used the React Native documentation to do this.

I have a broadcast receiver

public class MessageReceivedReceiver extends BroadcastReceiver {
    private static final String TAG = "MessageReceivedReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive");
        if (!isAppOnForeground((context))) {
            Log.i(TAG, "Not in foreground");
            /**
             We will start our service and send extra info about
             network connections
             **/
            Intent serviceIntent = new Intent(context, JSBackgroundService.class);
            serviceIntent.putExtras(intent.getExtras());
            context.startService(serviceIntent);
            HeadlessJsTaskService.acquireWakeLockNow(context);
        }
    }
...

A HeadlessJS Task Service:

public class JSBackgroundService extends HeadlessJsTaskService {
    final static String TAG = "JSBackgroundService";
    @Override
    protected @Nullable
    HeadlessJsTaskConfig getTaskConfig(Intent intent) {
        Bundle extras = intent.getExtras();
        Log.i(TAG, String.format("getTaskConfig: %s", extras));
        if (extras != null) {
            return new HeadlessJsTaskConfig(
                    "Test",
                    Arguments.fromBundle(extras),
                    5000, // timeout for the task
                    false // optional: defines whether or not  the task is allowed in foreground. Default is false
            );
        }
        return null;
    }
}```

My MainApplication implements ReactApplication:

public class MainApplication extends ExpoApplication implements ReactApplication {

private static final String TAG = MainApplication.class.getSimpleName();

private List<ReactPackage> packages = Arrays.<ReactPackage>asList(

// new MainReactPackage(),
new MyReactPackage()
);

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
        return packages;
    }
};

@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}


In App.js I have:


import JSBackgroundService from ‘./services/jsbackgroundservice’;
AppRegistry.registerHeadlessTask(‘Test’, () => {console.log(“Got it”); return JSBackgroundService; });

export default class App extends React.Component {
…```

And JsBackgroundService:

module.exports = async (e) => {
    // do stuff
    console.log("Running the background service");
    console.log(e);
  };

Based on logs I know that getTaskConfig is executed but for some reason, I don’t see anything in the console logs, looks like the javascript never gets executed. I have tried also running the Headless JS in foreground for testing purposes but to no avail.

Does anyone have any ideas what could I be doing wrong?

2 Likes
#2

I have debugged the problem a bit further and it seems that this line of code should get executed but never does. In other words the react context does not get initialized.

Any advices?

1 Like
closed #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.