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

Ability to stop foreground service when app is closed from recents #342

Closed
radko93 opened this issue Mar 18, 2022 · 13 comments
Closed

Ability to stop foreground service when app is closed from recents #342

radko93 opened this issue Mar 18, 2022 · 13 comments

Comments

@radko93
Copy link

radko93 commented Mar 18, 2022

I've been trying to use foreground services and managed to run things in the background. The problem is that's not always that easy to stop the service. When the app is "swiped out" from recents the service is still running, but the React Native context seems to be gone which is causing trouble.

Android potentially offers stopping the service. Seems like adding onTaskRemoved and stopping the service there might help. Someone posted this. How this could be integrated into the service in Notifee? I'm happy to contribute but I'm not sure how to make it configurable from JS side.

@vincent-paing
Copy link
Contributor

vincent-paing commented Mar 19, 2022

Foreground service are meant to live beyond the activity's lifecycle and used only for long running task that outlive the UI, which means it's meant to survive even after the app is cleared from recent. I'm not sure if foreground service is the correct use case for your scenario. Seems like it would be better to just show progress on the component itself since it's killed after app is no longer active in foreground.

but the React Native context seems to be gone

Do you have a reproducible example for this? I just checked the code and It's running a headless Task so you should be able to execute your task even if the app is no longer in active state.

With that being said, a quick hack would be to add android:stopWithTask="true" to notifee's Foreground service. (I haven't tested this out yet, so let me know if it doesn't work)

<application>
    <service android:name="app.notifee.core.ForegroundService"
        android:stopWithTask="true" />
</application>

@radko93
Copy link
Author

radko93 commented Mar 19, 2022

My app is streaming sound and I have to keep it running in case an audio stream changes. So I'm not sure if there's a better way to do this than run in Foreground Service.

When starting the foreground service my whole app is running (all network requests etc). But when I close the app from recents, UI is unmounted but seems like rest of the code is still running. This causes trouble - for example setting React state will fail. I would like to simply avoid this behaviour. I don't need the app running when the user wants to stop it manually.

Right now it seems that that adding stopWithTask did not help (at least on Android 12). When running locally I can see console output after closing the app from drawer.

@jzamarioli
Copy link

Assuming your notifications have asForegroundService: true
Have you tried to stop the foreground service on component unmount?
It should kill your pending notifications.
Something like this:

    useEffect(() => {

        return async () => {
            await notifee.stopForegroundService();
        };
    }, []);

@radko93
Copy link
Author

radko93 commented Mar 19, 2022

Yes I'm doing that already (and the notification is gone). But the Headless seems to be going. One example:
I have a hook that subscribes to app state and it shows a notifications when you background the app. It's still displayed 2-3 seconds after swiping of the app. Similar issue: voximplant/react-native-foreground-service#33

@radko93
Copy link
Author

radko93 commented Mar 19, 2022

PR that potentially could be introduced voximplant/react-native-foreground-service#32

@github-actions
Copy link

github-actions bot commented Dec 6, 2022

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 21, 2022
@gytiskv
Copy link

gytiskv commented Feb 24, 2024

Hi, did you find any solution for this?
android:stopWithTask="true" does not help, nor the await notifee.stopForegroundService();.
The notification seems to be killed, and the foreground service too, but some processes are still running and the app cannot be "killed" fully.

@supermegazinc
Copy link

Im having the same issue, the process never completely ends

@vincent-paing
Copy link
Contributor

vincent-paing commented Apr 23, 2024

Hi, did you find any solution for this? android:stopWithTask="true" does not help, nor the await notifee.stopForegroundService();. The notification seems to be killed, and the foreground service too, but some processes are still running and the app cannot be "killed" fully.

Foreground service and app process are two different things. Android OS handle the lifecycle of your app for you, so naturally the best practices is to let the OS handle it. I don't think it's the responsibility of a notification library to expose a function for you to kill the app since that's an entirely different thing.

@dkornilove
Copy link

I'm encountering an opposite issue where, after specifying asForegroundService: true to initiate notifications, the foreground service and associated notification disappear when I close the app from the recent apps list. This behavior began after upgrading from React Native version 0.71.6 to 0.74.1. In the earlier version, the foreground service would remain active even after closing the app. Could anyone provide assistance in understanding this new behavior, please?

@ibrust
Copy link

ibrust commented Jun 19, 2024

The behavior described - with the foreground service still running and keeping the app process alive after the app has been dismissed - is the behavior of foreground services in Android. But when you say the React Native context is gone - this is confusing. Foreground services use the same process as the application process. Infact you can access application state from within your foreground service, this is even true after the application has been swiped away in recents - the process has not been killed if a foreground service is still running.

From the docs:
"Most confusion about the Service class actually revolves around what it is not:

A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors)."

If you want to manually alter this behavior and kill the process when the user swipes away from recents there's an answer here describing how - https://stackoverflow.com/questions/19568315/how-to-handle-running-service-when-app-is-killed-by-swiping-in-android/26882533#26882533

I can't speak to what some react native library is doing...

@jainAbhishek-02
Copy link

Foreground service are meant to live beyond the activity's lifecycle and used only for long running task that outlive the UI, which means it's meant to survive even after the app is cleared from recent. I'm not sure if foreground service is the correct use case for your scenario. Seems like it would be better to just show progress on the component itself since it's killed after app is no longer active in foreground.

but the React Native context seems to be gone

Do you have a reproducible example for this? I just checked the code and It's running a headless Task so you should be able to execute your task even if the app is no longer in active state.

With that being said, a quick hack would be to add android:stopWithTask="true" to notifee's Foreground service. (I haven't tested this out yet, so let me know if it doesn't work)

<application>
    <service android:name="app.notifee.core.ForegroundService"
        android:stopWithTask="true" />
</application>

setting android:stopWithTask="true" is not working. Please suggest any other solution.

@thuongtv-vn
Copy link

I am facing the same issue here.
Is it safe to use android.os.Process.killProcess()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants