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

Fix background for android 8+ #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

EJohnF
Copy link

@EJohnF EJohnF commented Nov 15, 2018

fix for #63

the solution is taken from this discussion vikeri/react-native-background-job#71 and this PR vikeri/react-native-background-job#73

@clytras
Copy link

clytras commented Nov 16, 2018

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
Copy link
Author

EJohnF commented Nov 16, 2018

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

@EJohnF
Copy link
Author

EJohnF commented Nov 16, 2018

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

@clytras
Copy link

clytras commented Nov 16, 2018

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
Copy link
Author

EJohnF commented Nov 20, 2018

@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
Copy link

bamlhs commented Apr 5, 2019

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

@bamlhs
Copy link

bamlhs commented Apr 5, 2019

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
Copy link

This fix working fine on Xiaomi Redmi 6A - 8.10

@qlerebours
Copy link

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

@mirhat
Copy link

mirhat commented Sep 16, 2019

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

@mathiasmoeller
Copy link

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

@mathiasmoeller
Copy link

@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:

  • "react-native": "0.59.5"
  • "react-native-background-task": "github:EJohnF/react-native-background-task"

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants