7

I am using Headless.js for running background services with React Native. We are facing quite a few issues with its usage. What are my options for running Android background services using React Native?

Gunner
  • 736
  • 11
  • 19

3 Answers3

1

add file name BackgroundAudio.java

import android.content.Intent;
import android.os.Bundle;

import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;

public class BackgroundAudio extends HeadlessJsTaskService {

    @Override
    protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            return new HeadlessJsTaskConfig(
                    "BackgroundAudio",
                    Arguments.fromBundle(extras),
                    5000);
        }
        return null;
    }
}

edit AndroidManifest.xml

<service android:name=".BackgroundAudio" android:enabled="true" android:label="BackgroundAudio" />

Then in my index.android.js :

import BackgroundAudio from './src/BackgroundAudio'
AppRegistry.registerHeadlessTask('BackgroundAudio', () => BackgroundAudio)

And lastly, BackgroundAudio.js file referenced in the index.android.js reads as such:

export async function BackgroundAudio (taskData) { 
    alert('BACKGROUND AUDIO')
}
shodiqul muzaki
  • 110
  • 3
  • 7
1

There's a couple of packages that have been created since you asked this question that could be helpful depending on your exact use case.

Specifically you can use react native queue with react native background task to easily schedule a background task to execute periodically (roughly every ~15 min your scheduled task will run for at most 30 seconds - use the queue to handle task timeout management) when your app is closed (this works cross platform for iOS and Android). However, if your intention is to have a service that is running constantly in the background, I'm not certain that is possible in the RN world (so far as the time of my post is concerned).

billmalarky
  • 920
  • 2
  • 12
  • 34
-3

My guess is you will have to write it yourself. Unfortunately Headless JS pauses upon task completion, so I'm not sure it's suitable.

You can do anything in your task as long as it doesn't touch UI: network requests, timers and so on. Once your task completes (i.e. the promise is resolved), React Native will go into "paused" mode (unless there are other tasks running, or there is a foreground app).

https://facebook.github.io/react-native/docs/headless-js-android.html

Update: it is possible to keep the process running if the app loses focus. I will have to look for the test app source, to provide an example.

As an aside, I eventually wrote a background service, to start on boot, in Android. React Native doesn't allow for this type of service, nor is it the intention of RN to create completely headless apps. AppRegistry:

AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));

SomeTaskName.js:

module.exports = async (taskData) => {
  // do stuff
};

Java API:

public class MyTaskService extends HeadlessJsTaskService {

  @Override
  protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
      return new HeadlessJsTaskConfig(
          "SomeTaskName",
          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;
  }
}

AndroidManifest.xml

<service android:name="com.example.MyTaskService" />

Example:

Intent service = new Intent(getApplicationContext(), MyTaskService.class);
Bundle bundle = new Bundle();

bundle.putString("foo", "bar");
service.putExtras(bundle);

getApplicationContext().startService(service);
Tim
  • 20
  • 4
  • plz add some code the link may dead over period of time – Neelay Srivastava Feb 02 '17 at 13:32
  • _Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline._ – Bugs Feb 02 '17 at 13:37
  • How will I get to know whether a service is running? I have tried with console log, but it is not working for me? Please help : ( – Prajna Oct 07 '20 at 06:20