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, to sync fresh data, handle push notifications, or play music.
The JS API
A task is a async function that you register on AppRegistry, similar to registering React applications:
AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
+import { AppRegistry } from "react-native";
+AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
Then, in SomeTaskName.js:
module.exports = async (taskData) => {
@@ -81,7 +82,15 @@
You can do anything in your task such as network requests, timers and so on, as long as it doesn't touch UI. 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).
The Java API
Yes, this does still require some native code, but it's pretty thin. You need to extend HeadlessJsTaskService and override getTaskConfig, e.g.:
-public class MyTaskService extends HeadlessJsTaskService {
+package com.your_application_name;
+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;
+import javax.annotation.Nullable;
+
+public class MyTaskService extends HeadlessJsTaskService {
@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {
diff --git a/docs/next/headless-js-android/index.html b/docs/next/headless-js-android/index.html
index bacc442d965..7d6603b9089 100644
--- a/docs/next/headless-js-android/index.html
+++ b/docs/next/headless-js-android/index.html
@@ -71,7 +71,8 @@
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, to sync fresh data, handle push notifications, or play music.
The JS API
A task is a async function that you register on AppRegistry, similar to registering React applications:
AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
+import { AppRegistry } from "react-native";
+AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
Then, in SomeTaskName.js:
module.exports = async (taskData) => {
@@ -81,7 +82,15 @@
You can do anything in your task such as network requests, timers and so on, as long as it doesn't touch UI. 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).
The Java API
Yes, this does still require some native code, but it's pretty thin. You need to extend HeadlessJsTaskService and override getTaskConfig, e.g.:
-public class MyTaskService extends HeadlessJsTaskService {
+package com.your_application_name;
+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;
+import javax.annotation.Nullable;
+
+public class MyTaskService extends HeadlessJsTaskService {
@Override
protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {