react-native-foreground-service icon indicating copy to clipboard operation
react-native-foreground-service copied to clipboard

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

Open roysG opened this issue 6 years ago • 30 comments

roysG avatar Feb 04 '19 15:02 roysG

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

lorenc-tomasz avatar Apr 08 '19 13:04 lorenc-tomasz

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

cristianocca avatar Apr 15 '19 22:04 cristianocca

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.

cristianocca avatar Apr 18 '19 02:04 cristianocca

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 ?

belgianGeek avatar May 14 '19 19:05 belgianGeek

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.

cristianoccazinsp avatar May 15 '19 04:05 cristianoccazinsp

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

ivkrpv avatar May 15 '19 14:05 ivkrpv

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

belgianGeek avatar May 17 '19 12:05 belgianGeek

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

cristianoccazinsp avatar May 17 '19 13:05 cristianoccazinsp

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

belgianGeek avatar May 17 '19 13:05 belgianGeek

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

cristianoccazinsp avatar May 17 '19 16:05 cristianoccazinsp

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

marcocastignoli avatar Sep 12 '19 08:09 marcocastignoli

Same question

rdgomt avatar Sep 28 '19 21:09 rdgomt

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

Yahyaali1 avatar Oct 08 '19 12:10 Yahyaali1

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 avatar Oct 08 '19 13:10 cristianoccazinsp

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

marcocastignoli avatar Oct 08 '19 13:10 marcocastignoli

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 avatar Oct 08 '19 13:10 cristianoccazinsp

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

Yahyaali1 avatar Oct 08 '19 14:10 Yahyaali1

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

marcocastignoli avatar Oct 08 '19 14:10 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.

cristianoccazinsp avatar Oct 08 '19 14:10 cristianoccazinsp

@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

Yahyaali1 avatar Oct 10 '19 08:10 Yahyaali1

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

marcocastignoli avatar Oct 12 '19 14:10 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.

bimix avatar Jan 29 '20 12:01 bimix

@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 avatar Apr 24 '20 10:04 shacharbuda

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

cristianoccazinsp avatar Apr 24 '20 14:04 cristianoccazinsp

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 avatar Apr 25 '20 00:04 bimix

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

bimix avatar Apr 25 '20 00:04 bimix

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 .

cristianoccazinsp avatar Apr 25 '20 00:04 cristianoccazinsp

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!

bimix avatar Apr 25 '20 00:04 bimix

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 .

cristianoccazinsp avatar Apr 25 '20 02:04 cristianoccazinsp

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

shacharbuda avatar Apr 29 '20 08:04 shacharbuda