Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless JS doesnt run background processes #27835

Closed
bimix opened this issue Jan 22, 2020 · 2 comments
Closed

Headless JS doesnt run background processes #27835

bimix opened this issue Jan 22, 2020 · 2 comments
Labels
Bug Ran Commands One of our bots successfully processed a command. Resolution: For Stack Overflow A question for Stack Overflow. Applying this label will cause issue to be closed. Resolution: Locked This issue was locked by the bot.

Comments

@bimix
Copy link

bimix commented Jan 22, 2020

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 Files\nodejs\node.EXE
npm: 6.9.0 - C:\Program Files\nodejs\npm.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');
 }
@bimix bimix added the Bug label Jan 22, 2020
@hramos hramos added the Resolution: For Stack Overflow A question for Stack Overflow. Applying this label will cause issue to be closed. label Jan 22, 2020
@react-native-bot react-native-bot added the Ran Commands One of our bots successfully processed a command. label Jan 23, 2020
@react-native-bot
Copy link
Collaborator

Please use Stack Overflow for this type of question.

@tkdaj
Copy link

tkdaj commented Mar 27, 2021

I think this may be because you are running the task immediately when starting the app, which means the app would be in the foreground. According to the docs here:
https://reactnative.dev/docs/headless-js-android
When you configure your HeadlessJS task, it has an optional fourth param:

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
        );

If it is set to false (by default) your app would crash.

@facebook facebook locked as resolved and limited conversation to collaborators Oct 2, 2021
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Oct 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Ran Commands One of our bots successfully processed a command. Resolution: For Stack Overflow A question for Stack Overflow. Applying this label will cause issue to be closed. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants