Giter VIP home page Giter VIP logo

Comments (30)

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024 8

from react-native-foreground-service.

marcocastignoli avatar marcocastignoli commented on June 24, 2024 7

I'm feeling stupid, but I still can't understand where should I put the code I want to run in foreground :S

from react-native-foreground-service.

cristianocca avatar cristianocca commented on June 24, 2024 4

Same question... Not even the demo runs a code at all, does it just show a notification and that's all?

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024 4

@shacharbuda if you want to run a "task" on the service, and intend to keep running it even if the app is killed, the task needs to run in a headless JS. Most of the time you "register" a headless JS code at module level (instead of inside a mounted component) because headless may continue to run even after your app is unmounted.

from react-native-foreground-service.

shacharbuda avatar shacharbuda commented on June 24, 2024 3

@Yahyaali1 I'm not sure if you can run a headless task from a button press without implementing some native code. Definitely not from this library.

However, you can achieve what you want with a foreground service. If you don't use a headless task, your app will most likely be killed when the user swipes it out. If a foreground service is running and you don't stop it on App unmount, the app might still run, but you need your code to run at module level. Again, timers won't work.

From my testing and my modified version of this code that uses headless tasks, you can keep your app running in background even if it is killed as long as the foreground service is running.

I'm sorry I can't give you a straight answer, you will definitely need to test it by yourself. What I can tell you is, fetch will continue to work in background if a foreground service is running, but timers won't.

@cristianoccazinsp
We use your library and it works great!
Can you explain what do you mean with

but you need your code to run at module level.

?

All we did is implement a js async function that resolves only on a button click (and maybe even never resolved) and it seems to work fine. Is this fine or we should do something more?

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024 3

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024 2

Notification works, but if you want to "schedule a task" you will need to pass it to the java service somehow. A headless task might not be needed after all since javascript code seem to run even in background as long as the foreground notification is visible. Be warned that timeouts, intervals and stuff will still not work.

For me the best was to still add java code that receives a request to execute a headless task, with an optional delay time.

from react-native-foreground-service.

marcocastignoli avatar marcocastignoli commented on June 24, 2024 2

@cristianoccazinsp Sorry but I tried doing this just to test what you are saying, but when I put the app in background the requests stop. Am I doing something wrong?

componentDidMount() {

        setInterval(() => {
            fetch('http://172.16.125.173:8888/heroes')
        }, 2000)
    }

from react-native-foreground-service.

bimix avatar bimix commented on June 24, 2024 2

For whoever struggling with this, please do not waste your time with this headless and other BS because none of them works. There is nothing that makes ur app work in the background without using native code. If u want ur app to run in the background, write native code.

from react-native-foreground-service.

bimix avatar bimix commented on June 24, 2024 2

@shacharbuda if you want to run a "task" on the service, and intend to keep running it even if the app is killed, the task needs to run in a headless JS. Most of the time you "register" a headless JS code at module level (instead of inside a mounted component) because headless may continue to run even after your app is unmounted.

NOPE, they don't! Only by writing native code works!

from react-native-foreground-service.

bimix avatar bimix commented on June 24, 2024 2

That is not true. The JS interpreter can definitely run in background as long as there's a foreground service running. It can be further improved with headlessJS if you need to continue running something after the app has been killed. El vie., 24 de abril de 2020 21:19, Burim Syla [email protected] escribió:

For whoever struggling with this, please do not waste your time with this headless and other BS because none of them works. There is nothing that makes ur app work in the background without using native code. If u want ur app to run in the background, write native code. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#3 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALU263AB6NAF6DRQYBW3TZTROIUARANCNFSM4GUFSHRA .

Tried it, wasted a month on it!

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024 1

from react-native-foreground-service.

lorenc-tomasz avatar lorenc-tomasz commented on June 24, 2024

The same question. Especially when I want to setTimeout in background.

from react-native-foreground-service.

cristianocca avatar cristianocca commented on June 24, 2024

Well. The code from here provides a good starting point. You can implement your actual code delegating stuff to a HeadlessJS task which can be started from this service, or you can make this service a headless task by itself.

from react-native-foreground-service.

belgianGeek avatar belgianGeek commented on June 24, 2024

Hi there !
I am asking myself the same question : how can I use this library to execute a background task ? Using Headless JS might be cool. But how can I launch my task with the methods provided here ?

Moreover, the weird thing is that no notification appears on my screen... Is it working for you ?

from react-native-foreground-service.

ivkrpv avatar ivkrpv commented on June 24, 2024

Same here. If anyone have any example of how to run the actual code in the foreground service it would be awesome.

from react-native-foreground-service.

belgianGeek avatar belgianGeek commented on June 24, 2024

For me the best was to still add java code that receives a request to execute a headless task, with an optional delay time.

Thanks for your answer, but I can't figure out how to do that. I'm new to React native and I don't have any knowledge in Java. I know this is not the right place to ask that, but maybe you could help me on this Stack Overflow post ? It's not specifically related to this packahe but if you could hel me, I would appreciate it very much.

Thank you 😉 .

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024

@Leroux47 you are right, that stack overflow questions is not really related. You don't want a foreground service for what you are trying to achieve there. As for the foreground service, although I copied lots of code from here, I had to end up with my own implementation and code so I'm not really using this library.

from react-native-foreground-service.

belgianGeek avatar belgianGeek commented on June 24, 2024

Ok, thank you for your help @cristianoccazinsp 😉 . Sorry for the inconvenience.

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024

@Leroux47 still took a look at your stack overflow issue, I hope that helped.

from react-native-foreground-service.

rdgomt avatar rdgomt commented on June 24, 2024

Same question

from react-native-foreground-service.

Yahyaali1 avatar Yahyaali1 commented on June 24, 2024

Still struggling with the same issue. Where do I place the code that I need to run in foreground service ?

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024

from react-native-foreground-service.

Yahyaali1 avatar Yahyaali1 commented on June 24, 2024

@cristianoccazinsp I am relatively new to java script therefore would appreciate little help.
As per my use case. I am trying to provide user with option to recover there data from server ( data size is huge and after successful fetch it is placed in local database therefore requires batch sqllite insertions) .
Keeping android limitations in mind, I would need to show a foreground notification as well while running a headless js task.
Is there a way to run headless task on button click ? Or I have to write a native module that will start the headless task ?

from react-native-foreground-service.

marcocastignoli avatar marcocastignoli commented on June 24, 2024

@cristianoccazinsp Thanks a lot! Now it works perfectly!!

from react-native-foreground-service.

cristianoccazinsp avatar cristianoccazinsp commented on June 24, 2024

@Yahyaali1 I'm not sure if you can run a headless task from a button press without implementing some native code. Definitely not from this library.

However, you can achieve what you want with a foreground service. If you don't use a headless task, your app will most likely be killed when the user swipes it out. If a foreground service is running and you don't stop it on App unmount, the app might still run, but you need your code to run at module level. Again, timers won't work.

From my testing and my modified version of this code that uses headless tasks, you can keep your app running in background even if it is killed as long as the foreground service is running.

I'm sorry I can't give you a straight answer, you will definitely need to test it by yourself. What I can tell you is, fetch will continue to work in background if a foreground service is running, but timers won't.

from react-native-foreground-service.

Yahyaali1 avatar Yahyaali1 commented on June 24, 2024

@cristianoccazinsp thank you for your comment. I tried both of these implementations

  1. Create a simple foreground notification and perform bulk operation. As the react bridge with android persists as we have a foreground service I was not facing any problems with long running task besides that it was blocking UI.
  2. Create a headless js task. I further added implementation for initiating this task on button click. You would need to create Native Module and expose method to java script for starting headless task service. This implementation allows fine control over the situation. Furthermore in handless task you can run timmer as well (as per the documentation). For communication back to javascript I have used events.
    More explanation on what is happening in the background

from react-native-foreground-service.

marcocastignoli avatar marcocastignoli commented on June 24, 2024

If you guys are interested I created an React Native app starting from this library that starts a foreground service listening for websocket messages. When a websocket message arrives a notification is showed. The objective of the app is to allow me to receive notifications from apps that doesn't send notifications without play services

client: https://github.com/marcocastignoli/splurt-client
server: https://github.com/marcocastignoli/splurt-server

if you have any question write to me on telegram: @marcocastignoli

from react-native-foreground-service.

bimix avatar bimix commented on June 24, 2024

@Yahyaali1 I'm not sure if you can run a headless task from a button press without implementing some native code. Definitely not from this library.

However, you can achieve what you want with a foreground service. If you don't use a headless task, your app will most likely be killed when the user swipes it out. If a foreground service is running and you don't stop it on App unmount, the app might still run, but you need your code to run at module level. Again, timers won't work.

From my testing and my modified version of this code that uses headless tasks, you can keep your app running in background even if it is killed as long as the foreground service is running.

I'm sorry I can't give you a straight answer, you will definitely need to test it by yourself. What I can tell you is, fetch will continue to work in background if a foreground service is running, but timers won't.

I have a foreground service running but app doesn't run in the background.

from react-native-foreground-service.

shacharbuda avatar shacharbuda commented on June 24, 2024

.... The JS interpreter can definitely run in background as long as there's a foreground service running. It can be further improved with headlessJS if you need to continue running something after the app has been killed.

This message made me fully understand why one might want to implement a headless JS task in addition to the "regular" JS code. Thanks Again!

from react-native-foreground-service.

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.