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 ANRs when running on android 8.0 or higher. #73

Merged
merged 1 commit into from
Mar 23, 2018

Conversation

sagium
Copy link
Contributor

@sagium sagium commented Feb 10, 2018

On VERSION.SDK_INT >= VERSION_CODES.O

Context must call startForegroundService()
Service must call startForeground() and also show a notification.

Resolves issue #71

Service must call startForeground and also show a notification.
@kviswanath
Copy link

@sagium Did this fix work for you when the app is terminated.

In my tests, when the application is in background the background service was invoked. The background service wasn't invoked when the app was swiped and terminated after launching and using the application atleast once. Please let me know if your test results were otherwise.

Thanks.

@sagium
Copy link
Contributor Author

sagium commented Feb 13, 2018

@kviswanath I tested using RN 0.52.2.

To be safe I checked all of the following before releasing my app, with my fix.

  • Emulators (APIs 22, 23, 24, 25, 26, 27)
  • Real devices (APIs 22, 23, 24)

I used to receive errors in my crashlytics on Oreo and pre-Oreo devices.
The Oreo was (of course) not implemented yet.
The pre-Oreo error was
Fatal Exception: java.lang.IllegalStateException:
Tried to start task **** while in foreground, but this is not allowed.
com.facebook.react.jstasks.HeadlessJsTaskContext.startTask (HeadlessJsTaskContext.java:92)

and yes, the error seemed to persist with any flag value of allowExecutionInForeground.
Maybe the newer libraries did the trick, but I have zero errors for all my users till now and no reports of the service not working.

Now for my use case (background network fetch and if we got what we were searching for, show a local notification, otherwise do nothing) is working as expected. The background job is registered on app start and keeps running every 15min (give or take), with the app killed or in background.

I cannot completely decipher the google docs on the use cases (>= Oreo). It seems that if your background service needs to interact with the user it has to be registered as foregroundService, but if your service does only background work it could start as simple service. I think the headlessJS is messing the flow a little bit and it is safer to use foregroundService either way, with the downside of the mandatory local notification, showing to the user your app is doing something in the background.

If anyone can test more, that would be great. Testing my use case was a success, I cannot guarantee it will work for any android. The java code in the fix was as generic as it could be.

@vikeri
Copy link
Owner

vikeri commented Mar 23, 2018

Sorry for extremely slow respose. Will merge this but something that would be nicer is if the text and the icon could be configurable. I'll add an issue for that.

@vikeri vikeri merged commit c09ffb2 into vikeri:master Mar 23, 2018
@sagium sagium deleted the android_api_26_27_compatibilty branch December 10, 2018 10:31
@kameswarans
Copy link

@kviswanath @sagium @vikeri
Hi Bro,
Background jobs work fine. But when I implement react native activity recognition it shows warning like this in OREO

warning:
W/ActivityManager: Background start not allowed: service Intent { cmp=employee/com.xebia.activityrecognition.DetectionService (has extras) } to employee/com.xebia.activityrecognition.DetectionService from pid=-1 uid=10093 pkg=employee

Please Help me to solve this Bro

Modules:-
https://github.com/Aminoid/react-native-activity-recognition

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

4 participants