Git Product home page Git Product logo

Comments (11)

friyiajr avatar friyiajr commented on May 10, 2024 1

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.

cortinico avatar cortinico commented on May 10, 2024

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.

friyiajr avatar friyiajr commented on May 10, 2024

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.

cortinico avatar cortinico commented on May 10, 2024

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.

friyiajr avatar friyiajr commented on May 10, 2024

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.

cortinico avatar cortinico commented on May 10, 2024

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 of startForegroundService. 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.

friyiajr avatar friyiajr commented on May 10, 2024

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.

friyiajr avatar friyiajr commented on May 10, 2024

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.

cortinico avatar cortinico commented on May 10, 2024

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.

friyiajr avatar friyiajr commented on May 10, 2024

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.

cortinico avatar cortinico commented on May 10, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.