react-native-foreground-service
react-native-foreground-service copied to clipboard
Where should i put the code that i want to execute in background?
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 ! 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 ?
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.
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 😉 .
@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 :wink: . 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 [email protected] 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 https://github.com/voximplant/react-native-foreground-service/issues/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?
componentDidMount() {
setInterval(() => {
fetch('http://172.16.125.173:8888/heroes')
}, 2000)
}
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 [email protected] 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 https://github.com/voximplant/react-native-foreground-service/issues/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. 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 ?
@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
- 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.
- 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
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
@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.
@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?
@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.
@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!
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 https://github.com/voximplant/react-native-foreground-service/issues/3#issuecomment-619289058, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALU263AB6NAF6DRQYBW3TZTROIUARANCNFSM4GUFSHRA .
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!
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 [email protected] 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 [email protected] 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) https://github.com/voximplant/react-native-foreground-service/issues/3#issuecomment-619289058>, 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 https://github.com/voximplant/react-native-foreground-service/issues/3#issuecomment-619291449, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALU263H7AUYLNYPIKP4PDPTROIVXNANCNFSM4GUFSHRA .
.... 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!