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 111 forks source link

Fix background for android 8+ #64

Open EJohnF opened 5 years ago

EJohnF commented 5 years ago

fix for https://github.com/jamesisaac/react-native-background-task/issues/63

the solution is taken from this discussion https://github.com/vikeri/react-native-background-job/issues/71 and this PR https://github.com/vikeri/react-native-background-job/pull/73

clytras commented 5 years ago

This fix crashes the app when the background task starts on Android for me. Besides the crash, I had to update SDK version in gradle to make it see Build.VERSION_CODES.O.

The logcat error when it crashes:

11-16 02:08:48.773 1328-1328/com.my.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.my.app, PID: 1328
    android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
11-16 02:08:48.774 1328-1328/com.my.app E/com.marianhello.logging.UncaughtExceptionLogger: FATAL EXCEPTION: mainandroid.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

EDIT A related subject on SO: https://stackoverflow.com/questions/44425584/context-startforegroundservice-did-not-then-call-service-startforeground

EJohnF commented 5 years ago

wow, thank you to find it out! I'll look what to do here

EJohnF commented 5 years ago

@clytras can you say pls, what SDK you was have to update? Didn't meet that problem

clytras commented 5 years ago

Of course. It wouldn't compile when running yarn run android throwing an error about not finding Build.VERSION_CODES.O. After searching I changed the targetSdkVersion and compileSdkVersion to 27 and it worked. Check my fork here: https://github.com/clytras/react-native-background-task/blob/master/android/build.gradle

EDIT Here is the error when compiling with yarn run android:

> Task :react-native-background-task:compileDebugJavaWithJavac FAILED
/Users/user/Projects/App/apps/native-nav/MyApp/node_modules/react-native-background-task/android/src/main/java/com/jamesisaac/rnbackgroundtask/RNJob.java:36: error: cannot find symbol
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                                        ^
  symbol:   variable O
  location: class VERSION_CODES
/Users/user/Projects/App/apps/native-nav/MyApp/node_modules/react-native-background-task/android/src/main/java/com/jamesisaac/rnbackgroundtask/RNJob.java:37: error: cannot find symbol
            context.startForegroundService(service);
                   ^
  symbol:   method startForegroundService(Intent)
  location: variable context of type Context
2 errors
EJohnF commented 5 years ago

@clytras I hope now it'll work just fine. For me, on the emulator, it doesn't send this error. Please, let me know if you face some other problems here

bamlhs commented 5 years ago

Hello @EJohnF I tested in real device Galaxy J2 Core SM-J260F - 8.1.0 and it's working fine

bamlhs commented 5 years ago

Hello @EJohnF I tested in real device Galaxy J2 Core SM-J260F - 8.1.0 and it's working fine

unfortunately it crashed after job executed

faizrktm commented 5 years ago

This fix working fine on Xiaomi Redmi 6A - 8.10

qlerebours commented 4 years ago

This is working fine on my Xiaomi mi5 too, Android 8.0.0

mirhat commented 4 years ago

Is there any progress on this. Is it going to be merged?

mathiasmoeller commented 4 years ago

This is approved but not merged. Is there a reason for this @mkamals @jamesisaac? Is the project still maintained?

mathiasmoeller commented 4 years ago

@clytras @EJohnF even when using EJohnF's fork with the fix I get the issue that the "Job is running" log but the task is not being executed. I call BackgroundTask.define directly in index.js on root level and BackgroundTask.schedule from inside a Compontent. The task stores a value in the AsyncStorage and shows a local Notification. The logs from adb show:

03-06 12:19:48.145 17652 17745 D BackgroundTask: Initializing
03-06 12:19:48.152 17652 17652 D BackgroundTask: Setting foreground true
03-06 12:19:50.318 17652 17745 D BackgroundTask: @ReactMethod BackgroundTask.schedule
03-06 12:20:28.616 17652 17652 D BackgroundTask: Setting foreground false
03-06 12:20:28.616 17652 17652 D BackgroundTask: Committing job schedule
03-06 12:27:59.771 19666 19721 D BackgroundTask: Job is running
03-06 12:28:31.448 19772 19822 D BackgroundTask: Job is running
03-06 12:29:33.026 19839 19892 D BackgroundTask: Job is running
03-06 12:31:44.688 19970 20070 D BackgroundTask: Job is running

But the react native debugger doen't show that it was reactivated and the Storage is empty and the notification is not shown.

My setup is currently being tested on Android (Oneplus 5t) with:

Any ideas what the problem could be? Would be super grateful for any advice since this is very important for me :)