-
Notifications
You must be signed in to change notification settings - Fork 83
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
Comments
The same question. Especially when I want to setTimeout in background. |
Same question... Not even the demo runs a code at all, does it just show a notification and that's all? |
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. |
Hi there ! Moreover, the weird thing is that no notification appears on my screen... Is it working for you ? |
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. |
Same here. If anyone have any example of how to run the actual code in the foreground service it would be awesome. |
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 😉 . |
@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. |
Ok, thank you for your help @cristianoccazinsp 😉 . Sorry for the inconvenience. |
@Leroux47 still took a look at your stack overflow issue, I hope that helped. |
I'm feeling stupid, but I still can't understand where should I put the code I want to run in foreground :S |
Same question |
Still struggling with the same issue. Where do I place the code that I need to run in foreground service ? |
You don't put any code. The app basically keeps running in background if
there's a foreground service running.
Something even better is to run a headless task so the code keeps running
even if the app is killed.
El mar., 8 de octubre de 2019 09:25, Yahya Ali <notifications@github.com>
escribió:
… Still struggling with the same issue. Where do I place the code that I
need to run in foreground service ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3?email_source=notifications&email_token=ALU263GJ3A5V73V2QYSYDGTQNR35NA5CNFSM4GUFSHRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAT665I#issuecomment-539488117>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ALU263HRFK4HX7OERPHAJXLQNR35NANCNFSM4GUFSHRA>
.
|
@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?
|
Intervals and timers do not work in background, it doesn't matter how you
do it, they just don't work, not even in a headless task. Just run a loop
with await + fetch (without timers) and you will see it working. Make sure
the notification is also visible on top, otherwise your service is not
really running.
What I did to achieve a timeout/interval that works, is to implement it on
Java. For that reason, I took the foreground service code from here and
made my own code in a way I would additionally pass a delay/timeout flag
and use a headless task. I have the code to do this here:
https://github.com/cristianoccazinsp/react-native-foreground-service . It
is not well documented nor I expect people to use it, but you might grab
some ideas from it.
…On Tue, Oct 8, 2019 at 10:46 AM Marco Castignoli ***@***.***> wrote:
@cristianoccazinsp <https://github.com/cristianoccazinsp> Sorry but I
just tried doing this just to test what you are saying, but when I put the
app in the background the requests stop. Am I doing something wrong?
componentDidMount() {
setInterval(() => {
fetch('http://172.16.125.173:8888/heroes')
}, 2000)
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3?email_source=notifications&email_token=ALU263ERNHMMYLPIAQ6H2F3QNSFKVA5CNFSM4GUFSHRKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAUHCDA#issuecomment-539521292>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ALU263DFUDNEYZ3EQ4QJBJDQNSFKVANCNFSM4GUFSHRA>
.
|
@cristianoccazinsp I am relatively new to java script therefore would appreciate little help. |
@cristianoccazinsp Thanks a lot! Now it works perfectly!! |
@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 thank you for your comment. I tried both of these implementations
|
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 if you have any question write to me on telegram: |
I have a foreground service running but app doesn't run in the background. |
@cristianoccazinsp
? 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? |
@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. |
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. |
NOPE, they don't! Only by writing native code works! |
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! |
You must be doing something wrong. I definitely got background JavaScript
to run. As long as you don't use timers and the foreground notification is
up, it should work. I've been using this to implement a background queue
upload service and it works perfectly most of the time.
El vie., 24 de abril de 2020 21:33, Burim Syla <notifications@github.com>
escribió:
… 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
***@***.*** escribió:
… <#m_1863428169411824595_>
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)
<#3 (comment)>>,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ALU263AB6NAF6DRQYBW3TZTROIUARANCNFSM4GUFSHRA
.
Tried it, wasted a month on it!
—
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/ALU263H7AUYLNYPIKP4PDPTROIVXNANCNFSM4GUFSHRA>
.
|
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! |
No description provided.
The text was updated successfully, but these errors were encountered: