React-native: Headless JS doesnt run background processes

Created on 22 Jan 2020  路  1Comment  路  Source: facebook/react-native

Can't make the background processes run using the official documentation of RN. When the app starts it shuts down immediately and doesn't display any error messages.

React Native version:
System:
OS: Windows 10 10.0.18362
CPU: (8) x64 Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
Memory: 1.69 GB / 7.91 GB
Binaries:
Node: 10.16.2 - C:\Program Filesnodejsnode.EXE
npm: 6.9.0 - C:\Program Filesnodejsnpm.CMD
SDKs:
Android SDK:
API Levels: 23, 27, 28, 29
Build Tools: 27.0.3, 28.0.3, 29.0.2
System Images: android-28 | Google Play Intel x86 Atom, android-29 | Google Play Intel x86 Atom
IDEs:
Android Studio: Version 3.4.0.0 AI-183.6156.11.34.5692245
npmPackages:
react: 16.9.0 => 16.9.0
react-native: ^0.61.4 => 0.61.4

Steps To Reproduce

  1. Follow steps (if you can because it lacks so much of explanations) of RN official documentation.
  2. Run the app.

Describe what you expected to happen: I expect to print a console message when I send app to background

Snack, code example, screenshot, or link to a repository:

MainApplication.java :

    @Override
    public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    initializeFlipper(this); // Remove this line if you don't want Flipper enabled

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

    getApplicationContext().startService(service);
  }

MyTaskService.java :

package com.myApp;

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;
import com.facebook.react.bridge.WritableMap;

public class MyTaskService extends HeadlessJsTaskService {

   @Override
  protected @Nullable HeadlessJsTaskConfig getTaskConfig(Intent intent) {

Bundle extras = intent.getExtras();
  WritableMap data = extras != null ? Arguments.fromBundle(extras) : null;
return new HeadlessJsTaskConfig(
    "SomeTaskName", // Use the registered headless Task here
    data,
    5000);
}
}

AndroidManifest.xml:

<service android:name="com.myApp.MyTaskService" />
Index.js (React-Native code):

AppRegistry.registerHeadlessTask('SomeTaskName', () => SomeTaskName);
SomeTaskName.js (RN code):

    module.exports = async (taskData) => {
    alert('test');
    console.log('Went to background');
 }
Bug Ran Commands For Stack Overflow

>All comments

Please use Stack Overflow for this type of question.

Was this page helpful?
0 / 5 - 0 ratings