transistorsoft / react-native-background-fetch

Periodic callbacks in the background for both IOS and Android
MIT License
1.43k stars 191 forks source link

Example project schedule doesn't work when app is terminanted #490

Open vladimir-vucetic opened 1 month ago

vladimir-vucetic commented 1 month ago

Your Environment

Expected Behavior

From example app create schedule on android. Kill the app. Lock phone. Schedule execute at specified time.

Actual Behavior

Example project schedulle save logs while app is in foreground or background. But when app is killed n\no log is saved.

Steps to Reproduce

  1. I downloaded example
  2. yarn install
  3. npx react-native run-android
  4. Open app, press schedule, terminate app, wait 5sec, open app, no log is saved

Context

Create schedule on android. Kill the app. Lock phone. Schedule execute at specified time.

Debug logs

04-10 16:47:26.159 16555 16555 D TSBackgroundFetch: ☯️  onStart
04-10 16:47:26.168 16555 16555 D TSBackgroundFetch: ☯️  onResume
04-10 16:47:26.559 16555 17588 D TSBackgroundFetch: [RNBackgroundFetch initialize]
04-10 16:47:29.913 16555 17589 I ReactNativeJS: Running "FetchDemo" with {"rootTag":1}
04-10 16:47:30.730 16555 17590 D TSBackgroundFetch: - configure
04-10 16:47:30.730 16555 17590 D TSBackgroundFetch: Re-configured existing task
04-10 16:47:30.731 16555 17590 I TSBackgroundFetch: - cancel taskId=react-native-background-fetch, jobId=999
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch: {
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "taskId": "react-native-background-fetch",
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "isFetchTask": true,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "minimumFetchInterval": 15,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "stopOnTerminate": false,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "requiredNetworkType": 0,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "requiresCharging": false,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "requiresDeviceIdle": false,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "requiresStorageNotLow": false,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "startOnBoot": true,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "jobService": "com.transistorsoft.rnbackgroundfetch.HeadlessTask",
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "forceAlarmManager": false,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "periodic": true,
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch:   "delay": -1
04-10 16:47:30.741 16555 17590 D TSBackgroundFetch: }
04-10 16:47:33.160 16555 17590 D TSBackgroundFetch: - registerTask: com.transistorsoft.customtask
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch: {
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "taskId": "com.transistorsoft.customtask",
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "isFetchTask": false,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "minimumFetchInterval": 15,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "stopOnTerminate": true,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "requiredNetworkType": 0,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "requiresCharging": false,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "requiresDeviceIdle": false,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "requiresStorageNotLow": false,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "startOnBoot": false,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "forceAlarmManager": true,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "periodic": false,
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch:   "delay": 5000
04-10 16:47:33.161 16555 17590 D TSBackgroundFetch: }
04-10 16:47:36.491 16555 16555 D TSBackgroundFetch: ☯️  onPause
04-10 16:47:36.491 16555 16555 D TSBackgroundFetch: ☯️  onStop
04-10 16:47:37.233 16555 16555 D TSBackgroundFetch: ☯️  HeadlessMode? true
04-10 16:47:38.625 17642 17642 D TSBackgroundFetch: - Background Fetch event received: com.transistorsoft.customtask
04-10 16:47:38.632 17642 17642 D TSBackgroundFetch: ☯️  onCreate
04-10 16:47:38.633 17642 17642 D TSBackgroundFetch: - Stopping on terminate
04-10 16:47:38.633 17642 17642 D TSBackgroundFetch: - stop: com.transistorsoft.customtask
04-10 16:47:38.635 17642 17642 D TSBackgroundFetch: - FetchAlarmReceiver finish
04-10 16:47:38.639 17642 17642 I TSBackgroundFetch: - cancel taskId=com.transistorsoft.customtask, jobId=0
04-10 16:47:38.684 17642 17642 D TSBackgroundFetch: ☯️  HeadlessMode? true
04-10 16:47:46.653 17642 17642 D TSBackgroundFetch: ☯️  onStart
04-10 16:47:46.660 17642 17642 D TSBackgroundFetch: ☯️  onResume
04-10 16:47:47.095 17642 17727 D TSBackgroundFetch: [RNBackgroundFetch initialize]
04-10 16:47:50.475 17642 17728 I ReactNativeJS: Running "FetchDemo" with {"rootTag":1}
04-10 16:47:51.252 17642 17729 D TSBackgroundFetch: - configure
04-10 16:47:51.252 17642 17729 D TSBackgroundFetch: Re-configured existing task
04-10 16:47:51.253 17642 17729 I TSBackgroundFetch: - cancel taskId=react-native-background-fetch, jobId=999
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch: {
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "taskId": "react-native-background-fetch",
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "isFetchTask": true,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "minimumFetchInterval": 15,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "stopOnTerminate": false,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "requiredNetworkType": 0,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "requiresCharging": false,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "requiresDeviceIdle": false,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "requiresStorageNotLow": false,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "startOnBoot": true,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "jobService": "com.transistorsoft.rnbackgroundfetch.HeadlessTask",
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "forceAlarmManager": false,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "periodic": true,
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch:   "delay": -1
04-10 16:47:51.257 17642 17729 D TSBackgroundFetch: }
04-10 16:47:53.724 17642 17729 D TSBackgroundFetch: - registerTask: com.transistorsoft.customtask
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch: {
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "taskId": "com.transistorsoft.customtask",
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "isFetchTask": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "minimumFetchInterval": 15,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "stopOnTerminate": true,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiredNetworkType": 0,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresCharging": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresDeviceIdle": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresStorageNotLow": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "startOnBoot": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "forceAlarmManager": true,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "periodic": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "delay": 5000
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch: }
04-10 16:47:57.295 17642 17642 D TSBackgroundFetch: ☯️  onPause
04-10 16:47:57.295 17642 17642 D TSBackgroundFetch: ☯️  onStop
04-10 16:47:58.087 17642 17642 D TSBackgroundFetch: ☯️  HeadlessMode? true
04-10 16:47:59.182 17772 17772 D TSBackgroundFetch: - Background Fetch event received: com.transistorsoft.customtask
04-10 16:47:59.187 17772 17772 D TSBackgroundFetch: ☯️  onCreate
04-10 16:47:59.190 17772 17772 D TSBackgroundFetch: - Stopping on terminate
04-10 16:47:59.190 17772 17772 D TSBackgroundFetch: - stop: com.transistorsoft.customtask
04-10 16:47:59.191 17772 17772 D TSBackgroundFetch: - FetchAlarmReceiver finish
04-10 16:47:59.195 17772 17772 I TSBackgroundFetch: - cancel taskId=com.transistorsoft.customtask, jobId=0
04-10 16:47:59.237 17772 17772 D TSBackgroundFetch: ☯️  HeadlessMode? true
04-10 16:51:37.577 13977 14032 E ReactNativeJS: Invariant Violation: Failed to call into JavaScript module method AppRegistry.startHeadlessTask(). Module has not been registered as callable. Registered callable JavaScript modules (n = 10): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient.
04-10 16:51:37.577 13977 14032 E ReactNativeJS:         A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes
04-10 16:52:07.567 13977 13977 D TSBackgroundFetch: onHeadlessJsTaskFinish: 6
04-10 16:52:37.598 13977 13977 D TSBackgroundFetch: - Background Fetch event received: react-native-background-fetch
04-10 16:52:37.606 13977 13977 D TSBackgroundFetch: onHeadlessJsTaskStart: 7
04-10 16:52:37.607 13977 14032 E ReactNativeJS: Invariant Violation: Failed to call into JavaScript module method AppRegistry.startHeadlessTask(). Module has not been registered as callable. Registered callable JavaScript modules (n = 10): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient.
04-10 16:52:37.607 13977 14032 E ReactNativeJS:         A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes
04-10 16:53:07.606 13977 13977 D TSBackgroundFetch: onHeadlessJsTaskFinish: 7
04-10 16:53:37.599 13977 13977 D TSBackgroundFetch: [BGTask] timeout: react-native-background-fetch
04-10 16:53:37.605 13977 13977 D TSBackgroundFetch: onHeadlessJsTaskStart: 8
04-10 16:53:37.608 13977 14032 E ReactNativeJS: Invariant Violation: Failed to call into JavaScript module method AppRegistry.startHeadlessTask(). Module has not been registered as callable. Registered callable JavaScript modules (n = 10): Systrace, JSTimers, HeapCapture, SamplingProfiler, RCTLog, RCTDeviceEventEmitter, RCTNativeAppEventEmitter, GlobalPerformanceLogger, JSDevSupportModule, HMRClient.
04-10 16:53:37.608 13977 14032 E ReactNativeJS:         A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native., js engine: hermes
vvusts commented 1 month ago

Only think that is wrong from what I can see in logs is when press schedule button I get this part of log:

04-10 16:47:53.727 17642 17729 D TSBackgroundFetch: {
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "taskId": "com.transistorsoft.customtask",
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "isFetchTask": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "minimumFetchInterval": 15,
**04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "stopOnTerminate": true,**
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiredNetworkType": 0,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresCharging": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresDeviceIdle": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "requiresStorageNotLow": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "startOnBoot": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "forceAlarmManager": true,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "periodic": false,
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch:   "delay": 5000
04-10 16:47:53.727 17642 17729 D TSBackgroundFetch: }
christocracy commented 1 month ago

In order for stopOnTerminate: false, you must necessarily also set enableHeadless: true.

Add those options here

vvusts commented 1 month ago

Ah I tried by adding only stopOnTerminate it work now, thanks.

And property on schedule periodic what it actually do? I set it to true expecting that it will then run schedule every 5sec but it seems it's not that?

github-actions[bot] commented 4 days ago

This issue is stale because it has been open for 30 days with no activity.