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

Where should i put the code that i want to execute in background? #3

Open
roysG opened this issue Feb 4, 2019 · 30 comments
Open

Where should i put the code that i want to execute in background? #3

roysG opened this issue Feb 4, 2019 · 30 comments

Comments

@roysG
Copy link

roysG commented Feb 4, 2019

No description provided.

@lorenc-tomasz
Copy link

lorenc-tomasz commented Apr 8, 2019

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

@cristianocca
Copy link

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

@cristianocca
Copy link

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.

@belgianGeek
Copy link

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 ?

@cristianoccazinsp
Copy link

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.

@ivkrpv
Copy link

ivkrpv commented May 15, 2019

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

@belgianGeek
Copy link

belgianGeek commented May 17, 2019

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 😉 .

@cristianoccazinsp
Copy link

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

@belgianGeek
Copy link

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

@cristianoccazinsp
Copy link

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

@marcocastignoli
Copy link

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

@rdgomt
Copy link

rdgomt commented Sep 28, 2019

Same question

@Yahyaali1
Copy link

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

@cristianoccazinsp
Copy link

cristianoccazinsp commented Oct 8, 2019 via email

@marcocastignoli
Copy link

marcocastignoli commented Oct 8, 2019

@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)
    }

@cristianoccazinsp
Copy link

cristianoccazinsp commented Oct 8, 2019 via email

@Yahyaali1
Copy link

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

@marcocastignoli
Copy link

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

@cristianoccazinsp
Copy link

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

@Yahyaali1
Copy link

@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

@marcocastignoli
Copy link

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

@bimix
Copy link

bimix commented Jan 29, 2020

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

@shacharbuda
Copy link

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

@cristianoccazinsp
Copy link

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

@bimix
Copy link

bimix commented Apr 25, 2020

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.

@bimix
Copy link

bimix commented Apr 25, 2020

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

@cristianoccazinsp
Copy link

cristianoccazinsp commented Apr 25, 2020 via email

@bimix
Copy link

bimix commented Apr 25, 2020

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 notifications@github.com 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!

@cristianoccazinsp
Copy link

cristianoccazinsp commented Apr 25, 2020 via email

@shacharbuda
Copy link

.... 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!

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

No branches or pull requests