-
Notifications
You must be signed in to change notification settings - Fork 17
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
Background worker always fails on android, when the app is closed #25
Comments
@WilliamSarkisjan how are you closing the app? What happens if you kill the app via adb: |
@peitschie I don't do anything special when closing it (I don't force kill it or something). I just close off the app from the recently open apps. As for the adb command, where/how do I run that? |
On some phones, this actually terminates all running processes, even background tasks. What happens if you relax the battery optimizations / task launching permissions for your app? See https://dontkillmyapp.com/ for good tips on how to do this for each device.
Sorry, it's a lot to slog through for a simple trial, but best place to find this is with Android docs: https://developer.android.com/tools/adb#directingcommands But, if you're trying to get background tasks going, chances are you'll need to get pretty familiar with how some of the Android native stuff works as there's quite a few gotchyas there. (disclaimer: I'm not affiliated with the Ionic team or similar... I'm just an old-hand at Android background processing general) |
@peitschie Thank you for the quick replies I appreciate it! I had already tried turning off all the battery optimization stuff and it didn't change anything unfortunately. The thing is that in the android studio logcat I can see that the worker process actually does get triggered, but it fails always fails. I will definitely look into the adb command and see if that will give me more insight. Thanks again :) |
As extra information, these are the logs from android studio logcat. As you can see at 11:17:28 and 11:34:52 the "WorkerWrapper" background tasks actually get scheduled, but they fail immediately. However if I leave my app on in the background they are succesfull....
I noticed that during succesfull runs, when the app is open in the background, [Runner Co...inds.task] is executed, which doesn't happen when the app is closed
|
Hello @WilliamSarkisjan! What version of Android is this, and what device manufacturer? You mentioned it works in the foreground - is the notification successfully displayed? |
@theproducer Hi! I have the Samsung Galaxy S22 ultra (SM-S908B) with android version 13. I have tried everything from giving unrestricted access to battery resources, to making the interval time of the background process longer, but nothing works. When i dispatch events from the app itself it works perfectly too |
Since its Android 13, did you configure permissions correctly? Additionally, try wrapping your code in a try / catch (with the catch containing a |
@theproducer I indeed have the 'schedule exact alarm' in my androidmanifest, and when the app is in the foreground the notification actually gets delivered. I just added the console.error with try catch, but no error is being thrown as I expected.. The only time the worker ever fails is when the app is closed |
Is there any update on this? |
I can observe on an Android emulator that a closed app (yes, Google calls it closing) leads to the background runner not working, ex. WM-WorkerWrapper: Worker result FAILURE for Work [ id=bda753d7-43f5-4b12-a3fc-8629e2d988e3, tags={ io.ionic.backgroundrunner.plugin.RunnerWorker, app.foo.bar } ] Is this a plugin bug/missing feature, or it's simply not possible for a background runner to work while the app is closed/killed? |
@zarko-tg Interesting, I looked a bit more into this based on that failure log, and it looks like you are right, "forced closing" an app will also kill any scheduled work manager tasks. It also appears that certain vendors modify the assumed behavior even more drastically: We have a call out to potential Android device limitations here, but we may have to expand that section with more detail. Im not going to close this just yet, as I have several Samsung devices on the way I want to test with. This plugin was initially tested with a Samsung S21 running Android 13, so I will go back and confirm that it still works (and document what device settings were set), and see if I can replicate that on the other devices. But in the end there may not be anything we can do about this, especially if its true that Samsung is killing even foreground services. If WorkManager's API will not work, it's likely that the other possible APIs are limited as well. |
@theproducer So, I'd even take a step back from mentioning other vendors, as the bug/limitation is apparent on stock/vanilla Android emulator. In my case I tested on Android 13. |
@theproducer are there any updates on this |
@zarko-tg @WilliamSarkisjan Additionally, I've put up a Background Runner Test App that uses BR in some sample scenarios. After turning off each manufacturers battery optimizations, the app has worked well in testing for months thus far, and updates were reliability performed even across device updates and restarts. And at least on a Samsung S21 - even after "closing" an app. If you continue to run into Work Manager failures, see if you can reproduce those issues with the test app and create new issues here, with the device manufacturer and model and Android version. |
In my background.js I have one simple event listener:
and have my capacitor.config.ts defined as follows:
When I open my app, and then navigate back to my phone home screen (with the app still being open in the background), the background worker always finishes succesfully, and the event listener I have defined is fired/executed correctly.
However, when I completely close my app (so my app itself is not running in the background), my background worker always fails. The worker does fire at the interval I have set, however it never actually executes my eventlistener (since the console.log I put at the beginning of the function never gets executed), and instead the android studio console says that the worker failed.
Is there something I am missing or is this a bug with the plugin?
The text was updated successfully, but these errors were encountered: