transistorsoft / react-native-background-fetch

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

Why Task Stop Working When App Terminate In Android #451

Closed xts-bit closed 2 weeks ago

xts-bit commented 11 months ago

Your Environment

Expected Behavior

It should upload the file to the cloud when i Terminate the app on Android when i call the uploadHandler function and. the data.message from the API is "successfully" it should call the ImageHandler function and run it in the background

Actual Behavior

It's not uploading files to the cloud and not hitting the API tried this thing many times and also check my server logs

Steps to Reproduce

  1. Call The uploadHandler
  2. Swipe up
  3. Swipe up the app
  4. App is closed

Context


const ImageHandler = async () => {
    return new Promise(async (resolve, reject) => {
        try {
            // Uploading Image to Cloud
            if (data.message === 'done successfully') {
                resolve();
            } else {
                reject(new Error('Something went wrong, please try again'));
            }
        } catch (error) {
            reject(error);
        }
    });
};

export default function App() {

    useEffect(() => {
        BackgroundFetch.configure(
            {
                minimumFetchInterval: 15,
                stopOnTerminate: false,
                startOnBoot: true,
                enableHeadless: true,
                requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY
            },
            async (taskId) => {
                console.log('[BackgroundFetch HeadlessTask] start');
                await uploadHandler(taskId);
            },
            (error) => {
                console.log('[BackgroundFetch] ERROR: ', error);
            }
        );
    }, []);

    const uploadHandler = async (_taskId) => {
            try {
               // Upload File Function
               if (data.message === 'successfully') {
              ImageHandler(image)
            }
            } catch (error) {
                console.log(error);
            }
        } else {
            alert('Please wait its processing');
        }
        BackgroundFetch.finish(taskId);
    };

    return (
       // JSX
    )
};

Debug logs

[:180 call()
05-27 22:13:30.698  1289  1340 I PeriodicStatsRunner: PeriodicStatsRunner.call():184 No submit PeriodicStats since input started.
05-27 22:13:34.488   320   320 I android.hardware.vibrator-service.example: Vibrator reporting capabilities
05-27 22:13:34.489   320   320 I android.hardware.vibrator-service.example: Vibrator off
05-27 22:13:34.489   320   320 I android.hardware.vibrator-service.example: Vibrator perform
05-27 22:13:34.490   320 29824 I android.hardware.vibrator-service.example: Starting perform on another thread
05-27 22:13:34.500   494  3453 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:34.500   494  3453 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:34.591   320 29824 I android.hardware.vibrator-service.example: Notifying perform complete
05-27 22:13:34.624   320   320 I android.hardware.vibrator-service.example: Vibrator off
05-27 22:13:34.636   494  2318 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:34.649   668   668 V ShadeControllerImpl: NotificationShadeWindow: com.android.systemui.statusbar.phone.NotificationShadeWindowView{e3a3082 I.E...... ......ID 0,0-1080,2280} canPanelBeCollapsed(): false
05-27 22:13:37.627   494  2318 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-27 22:13:40.580  1134 29731 I AlarmManager: setExactAndAllowWhileIdle [name: GCM_HB_ALARM type: 2 triggerAtMillis: 7076945]
05-27 22:13:40.590  1134 29731 E WakeLock: GCM_HB_ALARM release without a matched acquire!
05-27 22:13:45.637  1380  1410 W hbox:interacto: Reducing the number of considered missed Gc histogram windows from 137 to 100
05-27 22:13:51.110 26292 26333 W earchbox:searc: Profile data size exceeds 500000 bytes. It has 552474 bytes.
^[[A05-27 22:13:54.304 27053 27109 I Finsky  : [1325] ivz.run(33): AIM: AppInfoManager-Perf > Destroying AppInfoManager ...
05-27 22:13:55.671   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup4: Permission denied
05-27 22:13:55.668   211   211 W Binder:211_2: type=1400 audit(0.0:1846): avc: denied { read } for name="wakeup4" dev="sysfs" ino=16464 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:13:55.671   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup2: Permission denied
05-27 22:13:55.668   211   211 W Binder:211_2: type=1400 audit(0.0:1848): avc: denied { read } for name="wakeup0" dev="sysfs" ino=13341 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:13:55.672   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup0: Permission denied
05-27 22:13:55.672   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup5: Permission denied
05-27 22:13:55.673   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup3: Permission denied
05-27 22:13:55.673   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup1: Permission denied
05-27 22:13:56.085   467   467 E netmgr  : qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
05-27 22:13:56.085   467   467 E netmgr  : Failed to open QEMU pipe 'qemud:network': Invalid argument
05-27 22:13:57.073   471   471 E wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
05-27 22:13:57.073   471   471 E wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
05-27 22:13:58.230 27053 27098 I Finsky  : [1316] ivz.run(33): AIM: AppInfoManager-Perf > Destroying AppInfoManager ...
05-27 22:14:06.600 26292 29828 I earchbox:searc: oneway function results will be dropped but finished with status UNKNOWN_TRANSACTION and parcel size 0
05-27 22:14:10.034  1007  1027 W System  : A resource failed to call release. 
05-27 22:14:10.035  1007  1027 I chatty  : uid=10153(com.google.android.apps.nexuslauncher) FinalizerDaemon identical 1 line
05-27 22:14:10.035  1007  1027 W System  : A resource failed to call release. 
05-27 22:14:11.796   211   211 W Binder:211_2: type=1400 audit(0.0:1852): avc: denied { read } for name="wakeup4" dev="sysfs" ino=16464 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.796   211   211 W Binder:211_2: type=1400 audit(0.0:1853): avc: denied { read } for name="wakeup2" dev="sysfs" ino=15902 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.796   211   211 W Binder:211_2: type=1400 audit(0.0:1854): avc: denied { read } for name="wakeup0" dev="sysfs" ino=13341 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.800   211   211 W Binder:211_2: type=1400 audit(0.0:1855): avc: denied { read } for name="wakeup5" dev="sysfs" ino=16527 scontext=u:r:system_suspend:s0 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0
05-27 22:14:11.801   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup4: Permission denied
05-27 22:14:11.802   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup2: Permission denied
05-27 22:14:11.802   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup0: Permission denied
05-27 22:14:11.803   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup5: Permission denied
05-27 22:14:11.803   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup3: Permission denied
05-27 22:14:11.804   211   215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup1: Permission denied
05-27 22:14:21.188   494  4816 W ActivityManager: Backgrou](url)
christocracy commented 11 months ago

Where’s your Debug Logs from $ adb logcat?

xts-bit commented 11 months ago

@christocracy i updated please check again i see there is a error "05-27 22:14:11.802 211 215 E android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup0: Permission denied"

christocracy commented 11 months ago

None of that references anything from the plug-in. See https://dontkillmyapp.com

xts-bit commented 11 months ago

@christocracy How to debug it? will it cause this problem in production ?

christocracy commented 11 months ago

I have no idea what those logs mean. I suggest you modify device settings according to https://dontkillmyapp.com.

xts-bit commented 11 months ago

@christocracy Also after running the app for 10min it gave this warning " WARN No task registered for key BackgroundFetch" any idea about can be the issue? is it common?

christocracy commented 11 months ago

I suggest you simplify your code. Comment-out all your “uploading” stuff and simply do a log statement.

them simulate tasks according to the Debugging section in README

xts-bit commented 11 months ago

@christocracy i tried remove all the upload stuff and just hit a api after 3 seconds it does any hit the api

christocracy commented 11 months ago

Your api calls don’t concern me.

I’m only interested in seeing your callback function print a message to the logs.

xts-bit commented 11 months ago

@christocracy it found this error for just console.log("Hello World") in background "05-28 15:32:40.212 3098 7224 E BgTaskExecutorImpl: EXCLUSIVE background task FETCH_CONFIGS_FROM_PHENOTYPE was cancelled. 05-28 15:32:40.212 3098 7224 E BgTaskExecutorImpl: java.util.concurrent.CancellationException: Task was cancelled."

Can i give a github repo of the project?

christocracy commented 11 months ago

Comment-out your entire BackgroundFetch.configure block and replace it with this:

BackgroundFetch.configure({
  minimumFetchInterval: 15,
  stopOnTerminate: false,
  startOnBoot: true,
  enableHeadless: true,
  requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY
}, async (taskId:string) => {
  console.log('[BackgroundFetch] taskId', taskId);                    
   BackgroundFetch.finish(taskId);
}, (taskId:string) => {  
  console.log('[Fetch] TIMEOUT taskId:', taskId);
  BackgroundFetch.finish(taskId);
});    

While simulating events according to the README, observe and provide the logcat results here:

$ adb logcat *:S ReactNative:V ReactNativeJS:V TSBackgroundFetch:V
xts-bit commented 11 months ago

@christocracy i cant see any message related to "Register Task" however i found out a log that says

 05-29 13:04:15.946  7778  7778 D TSBackgroundFetch: ☯️  onPause
05-29 13:04:15.946  7778  7778 D TSBackgroundFetch: ☯️  onStop
christocracy commented 11 months ago

What exactly are you executing to simulate the task?

xts-bit commented 11 months ago

@christocracy I am just uploading image to cloud for test purpose I am logging hello world in background

christocracy commented 11 months ago

I’m not talking about your “upload” code.

what is the adb command you’re using to simulate a fetch event. As documented in the readme.

xts-bit commented 11 months ago

@christocracy Chris it logs this:

05-29 20:40:26.666 11384 11447 D TSBackgroundFetch: - configure
05-29 20:40:26.666 11384 11447 D TSBackgroundFetch: - start
05-29 20:40:26.669 11384 11384 D TSBackgroundFetch: ☯️  onCreate
05-29 20:40:26.669 11384 11384 D TSBackgroundFetch: ☯️  onStart
05-29 20:40:26.669 11384 11384 D TSBackgroundFetch: ☯️  onResume
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch: - registerTask: react-native-background-fetch (jobId: 999)
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch: {
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "taskId": "react-native-background-fetch",
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "isFetchTask": true,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "minimumFetchInterval": 15,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "stopOnTerminate": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiredNetworkType": 1,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresCharging": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresDeviceIdle": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "requiresStorageNotLow": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "startOnBoot": true,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "jobService": "com.transistorsoft.rnbackgroundfetch.HeadlessTask",
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "forceAlarmManager": false,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "periodic": true,
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch:   "delay": -1
05-29 20:40:26.676 11384 11447 D TSBackgroundFetch: }
05-29 20:40:28.867 11384 11384 D TSBackgroundFetch: ☯️  onPause
05-29 20:40:28.867 11384 11384 D TSBackgroundFetch: ☯️  onStop
christocracy commented 11 months ago

Are you not aware you can SIMULATE fetch events?

read the README to learn how to simulate events.

IMG_0630

xts-bit commented 11 months ago

@christocracy yep it logs this "Running job [FORCED]"

christocracy commented 11 months ago

Print the logs after simulating an event.

xts-bit commented 11 months ago

@christocracy Can you please tell me what if mean about "Print the logs after simulating an event"

i run this "adb shell cmd jobscheduler run -f 999"

christocracy commented 11 months ago

“Print the logs” = adb logcat *:S ReactNative:V ReactNativeJS:V TSBackgroundFetch:V

xts-bit commented 11 months ago

@christocracy When i run adb logcat '*:S ReactNative:V ReactNativeJS:V TSBackgroundFetch:V' it log these messages related to TSBackgroundFetch:

05-29 21:26:58.960 14251 14251 D TSBackgroundFetch: ☯️  onCreate
05-29 21:26:58.960 14251 14251 D TSBackgroundFetch: ☯️  onStart
05-29 21:26:58.960 14251 14251 D TSBackgroundFetch: ☯️  onResume
05-29 21:26:58.960 14251 14315 D TSBackgroundFetch: - configure
05-29 21:26:58.960 14251 14315 D TSBackgroundFetch: - start
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch: - registerTask: react-native-background-fetch (jobId: 999)
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch: {
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "taskId": "react-native-background-fetch",
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "isFetchTask": true,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "minimumFetchInterval": 15,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "stopOnTerminate": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiredNetworkType": 1,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresBatteryNotLow": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresCharging": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresDeviceIdle": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "requiresStorageNotLow": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "startOnBoot": true,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "jobService": "com.transistorsoft.rnbackgroundfetch.HeadlessTask",
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "forceAlarmManager": false,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "periodic": true,
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch:   "delay": -1
05-29 21:26:58.961 14251 14315 D TSBackgroundFetch: }
christocracy commented 11 months ago

i run this "adb shell cmd jobscheduler run -f 999"

Are you replacing <your.application.id> with your app’s actual application id?

xts-bit commented 11 months ago

@christocracy maybe i am doing wrong is this correct?

james@Jamess-MacBook-Air FoodApp % adb shell cmd jobscheduler run -f com.foodapp  999
Running job [FORCED]
james@Jamess-MacBook-Air FoodApp % 
xts-bit commented 11 months ago

@christocracy Can you give me a simple rn component code snippet that i can copy test and check for any problem with my Android emulator?

christocracy commented 11 months ago

Can you give me a simple rn component code snippet that i can copy test

That’s exactly what I posted above.

xts-bit commented 11 months ago

@christocracy I already tried that code can you tell what is a positive log that I can see on console to assure that things are correct

christocracy commented 11 months ago

See https://dontkillmyapp.com

christocracy commented 11 months ago

what is a positive log that I can see on console to assure that things are correct

TSBackgroundFetch: - Background Fetch event received: react-native-background-fetch
ReactNativeJS: '[BackgroundFetch]', 'react-native-background-fetch' <------------------- this is a console.log
TSBackgroundFetch: - finish: react-native-background-fetch
TSBackgroundFetch: - jobFinished
github-actions[bot] commented 4 weeks ago

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

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been inactive for 14 days since being marked as stale.