jamesisaac / react-native-background-task

Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
MIT License
1.1k stars 110 forks source link

No task registered for key BackgroundTask #15

Open parohy opened 6 years ago

parohy commented 6 years ago

Hello. This is android related issue. I experienced this behavior after starting up the emulator when the app was installed on it before. I saw in the logs that the Job is starting. After a minute or tho I get the following crash: App crash

Also some of the engineers reported with release builds that they got few times the crash dialog "App stopped working" while using the phone. We are defining the task inside root component (the one you register in AppRegistry) in componentDidMount() function.

"react": "16.0.0", "react-native": "0.49.3", "react-native-background-fetch": "2.2.1", "react-native-background-task": "0.2.1"

jamesisaac commented 6 years ago

Please see the note in the readme about where tasks must be defined:

Should be called at the top level of your JS, not inside a component. This is because in headless mode no components are mounted, but the code still needs to be accessible.

If you need to communicate back and forth between your component in the foreground and headless mode, the technique I use is sticking some data in AsyncStorage and reading it out again.

parohy commented 6 years ago

@jamesisaac Thanks for answering :) So should I define it after imports in file where root component is?

jamesisaac commented 6 years ago

Yup. See the examples at the bottom of the readme. No matter how complex your task is, it should be defined like that.

EricYuen commented 6 years ago

I'm experiencing the same bug, however it is not always reproducible. The task schedules and runs occasionally, but would crash occasionally and unpredictably. Have you been able to resolve it, if so please provide an example.

Note: I'm also using react-native-navigation module my index.js looks as follows

import { AppRegistry } from 'react-native';
import {Navigation, NativeEventsReceiver} from 'react-native-navigation';

AppRegistry.registerHeadlessTask('BootUpTask', () => require('./app/BootUpTask'));

Promise.resolve(Navigation.isAppLaunched())
  .then(appLaunched => {
    if (appLaunched) {
      App.launch();
    } else {
      new NativeEventsReceiver().appLaunched(App.launch); // App hasn't been launched yet -> show the UI only when needed.
    }
  });
Zilleabbas10 commented 4 years ago

I am also facing the same issue when app is killed forcefully. It is working fine when app is in background. Here are my logs

01-29 16:32:21.885 10308 10550 W ReactNativeJS: registerHeadlessTask or registerCancellableHeadlessTask called multiple times for same key 'BackgroundTask'
01-29 16:32:21.909 10308 10550 I ReactNativeJS: 'text strong is ====>>>   ', ''
01-29 16:32:21.913 10308 10308 D BackgroundTask: Setting foreground true
01-29 16:32:21.932 10308 10551 D BackgroundTask: @ReactMethod BackgroundTask.schedule
01-29 16:32:21.938 10308 10550 I ReactNativeJS: { available: true }
01-29 16:32:22.100 10308 10308 D BackgroundTask: Setting foreground false
01-29 16:32:22.100 10308 10308 D BackgroundTask: Committing job schedule
01-29 16:42:22.620 16094 16112 D BackgroundTask: Job is running
01-29 16:42:22.712 16094 16094 D BackgroundTask: Returning HeadlessJsTaskConfig, timeout=30 ms
01-29 16:42:22.760 16094 16094 D ReactNative: ReactInstanceManager.ctor()
01-29 16:42:22.826 16094 16094 D ReactNative: ReactInstanceManager.createReactContextInBackground()
01-29 16:42:22.826 16094 16094 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
01-29 16:42:23.167 16094 16094 D ReactNative: ReactInstanceManager.onJSBundleLoadedFromServer()
01-29 16:42:23.173 16094 16094 D ReactNative: ReactInstanceManager.recreateReactContextInBackground()
01-29 16:42:23.174 16094 16094 D ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread()
01-29 16:42:23.184 16094 16139 D ReactNative: ReactInstanceManager.createReactContext()
01-29 16:42:23.233 16094 16139 D ReactNative: Initializing React Xplat Bridge.
01-29 16:42:23.242 16094 16139 D ReactNative: Initializing React Xplat Bridge before initializeBridge
01-29 16:42:23.262 16094 16139 D ReactNative: Initializing React Xplat Bridge after initializeBridge
01-29 16:42:23.263 16094 16139 D ReactNative: CatalystInstanceImpl.runJSBundle()
01-29 16:42:23.264 16094 16141 D ReactNative: ReactInstanceManager.setupReactContext()
01-29 16:42:23.264 16094 16141 D ReactNative: CatalystInstanceImpl.initialize()
01-29 16:42:23.329 16094 16141 D BackgroundTask: Initializing
01-29 16:42:23.804 16094 16140 W ReactNativeJS: No task registered for key BackgroundTask