Comments (11)
Thanks @cortinico, I think I'll just create that documentation issue at this point. I won't be the one to pick it up though.
I've spent probably 15 hours on this now and I feel like I am not getting anywhere 😅. I don't think I am going to figure this out on my own. The Android solutions I keep seeing are increasingly complex and I can't figure out how to integrate them all.
Thank you for your patience though, sorry I couldn't help more. Here is the issue: facebook/react-native-website#4079
from react-native.
Thanks for providing a reproducer @friyiajr this really helps to understand where is the problem.
The problem is here:
https://github.com/friyiajr/HeadlessJsReproducer/blob/c09cfc83df5e1f509c50111a77d58dac15f2b93d/android/app/src/main/java/com/wa2goose/BackgroundBLE/PulseReceiver.java#L21
For Android 8.0 devices, you have to call startForegroundService
, as suggested here:
https://stackoverflow.com/a/47654126
from react-native.
Thanks @cortinico would it make sense to update the React-Native documentation on this since the docs say?
Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example, to sync fresh data, handle push notifications, or play music.
And this is impossible on modern Android without using startForegroundService
.
I haven't had a chance to look into this much yet. But it does seem like it has a much different set or rituals and boilerplate then just calling startService
does.
If I can get this to work in my project, I don't mind making a PR for this
from react-native.
And this is impossible on modern Android without using
startForegroundService
.
I don't understand why you say that the documentation needs to be updated. Android has changed and requires different APIs to do computation in the background but the statement about about Headless JS still holds true
from react-native.
First, thanks for being so responsive on this! I really appreciate it.
The example provided in the documentation uses startService
in this section instead of startForegroundService
. If you want to do what the first sentence of the article is describing which is:
Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example, to sync fresh data, handle push notifications, or play music.
This is not possible unless you update the example to use startForegroundService
.
I don't have as much context as others who have come from a pure Android background and I am sure I am not alone in this. Its very easy for others who've done mostly React-Native or Expo to follow this documentation as-is and come away with the impression that Headless JS has regressed or doesn't work because they are trying to use startService
like the documentation tells them to do.
To do exactly what is described in that first sentence there are a lot of permissions to add and steps to take in your Android code. See: https://medium.com/@domen.lanisnik/guide-to-foreground-services-on-android-9d0127dc8f9a
from react-native.
First, thanks for being so responsive on this! I really appreciate it.
<3
The example provided in the documentation uses
startService
in this section instead ofstartForegroundService
. If you want to do what the first sentence of the article is describing which is:
Ah yes you're right, the snippets needs to be adapted. Don't you mind opening an issue on https://github.com/facebook/react-native-website/issues/ ?
If you're up also for sending a PR that updates them we can take the conversation forward over there
from react-native.
Sounds good to me! I'll create an issue later today. I am going to start working on a documentation update soon! This sounds like fun.
It will probably take a few weeks to finish since I need to figure this out myself 😅 and this is a side project. If you feel like this is easy for you though and you want to pick it up ahead of me I won't be offended.
from react-native.
So @cortinico it looks like this could be an actual bug in React-Native after all. I made changes so that I could use startForegroundService
instead of startService
. And the HeadlessJsTaskService
no longer wakes up the JavaScript code at all.
I created a new branch and have a diff here that hopefully is useful. It could be a code error on my part or a lack of knowledge in Android. Please let me know.
from react-native.
And the
HeadlessJsTaskService
no longer wakes up the JavaScript code at all.
Are there any crash or relevant logs I could look into?
from react-native.
This is the one I am seeing. We need to start a foreground service when the app is in the foreground, but its weird because HeadlessJS forces you to have things be background only.
Logs are below the code
@RequiresApi(Build.VERSION_CODES.O)
override fun getTaskConfig(intent: Intent): HeadlessJsTaskConfig? {
return intent.extras?.let {
HeadlessJsTaskConfig(
"PulseCheck",
Arguments.fromBundle(it),
5000,
false,
NoRetryPolicy.INSTANCE
)
}
}
2024-04-10 11:17:44.052 9978-10100 AndroidRuntime com.wa2goose.BackgroundBLE E FATAL EXCEPTION: Timer-0
Process: com.wa2goose.BackgroundBLE, PID: 9978
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.wa2goose.BackgroundBLE/.PulseCheck
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelableInternal(Parcel.java:5015)
at android.os.Parcel.readParcelable(Parcel.java:4997)
at android.os.Parcel.createExceptionOrNull(Parcel.java:3177)
at android.os.Parcel.createException(Parcel.java:3166)
at android.os.Parcel.readException(Parcel.java:3149)
at android.os.Parcel.readException(Parcel.java:3091)
at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6084)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1932)
at android.app.ContextImpl.startForegroundService(ContextImpl.java:1907)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:831)
at com.wa2goose.BackgroundBLE.MainActivity$onCreate$1.run(MainActivity.kt:58)
at java.util.TimerThread.mainLoop(Timer.java:563)
at java.util.TimerThread.run(Timer.java:513)
from react-native.
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.wa2goose.BackgroundBLE/.PulseCheck
This is again an issue with Android and not an issue with React Native itself.
See here for more context: https://stackoverflow.com/a/72131422
from react-native.
Related Issues (20)
- `fontVariant` doesn't work on iOS when using the new architecture
- UI problem with iOS floating keyboard in iPad. HOT 7
- TouchableWithoutFeedback does not support style HOT 2
- Cannot find EventEmitter for receivedTouches EventName[topTouchMove, topTouchEnd] on native library HOT 5
- RCTNativeAnimatedNodesManager.mm props is not a node HOT 1
- Unexpected scroll behaviors when using FlatList HOT 7
- When I integration react native with Existing Android Apps,I got a autolinking question. HOT 2
- Symbol not found: (_JSGlobalContextSetInspectable) HOT 3
- The directory ****/ios contains 2 projects HOT 3
- textInput props textAlign will cause long text to be cut off after the hyphen when out of focus and focus again
- close the first modal with animation, then open the second modal immediately, the second modal can't be showed HOT 2
- UI updates made from layout effect are flushed in separate UI transaction HOT 4
- Blob constructor is not spec compliant HOT 1
- [!] CocoaPods could not find compatible versions for pod "React-RCTAnimation": In Podfile: React-RCTAnimation (from `../node_modules/react-native/Libraries/NativeAnimation`) HOT 3
- Build error in 0.73.6 HOT 3
- Fatal error "available height is undefined" when editing textinput the app closes HOT 3
- Inverted Flatlist not working when scrolled HOT 5
- `TextInput` cursor lags behind text when component is controlled if Auto-Correction and Predictive Text are enabled HOT 6
- RNSVG same register view HOT 4
- Firestore encountered gRPC-C++ error while upgrading Firebase module to 19.2(iOS) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native.