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

feat: Really background tasks #3032

Closed
nicobytes opened this issue Jun 1, 2020 · 28 comments
Closed

feat: Really background tasks #3032

nicobytes opened this issue Jun 1, 2020 · 28 comments

Comments

@nicobytes
Copy link

nicobytes commented Jun 1, 2020

Feature Request

Hello!

The current background task is limited only run tasks in IOS for short time I understand the limitations but how the timer apps?

But in IOS my timer stops in Cordova I use the background-mode-plugin and works but with Capacitor what is the better approach?

I tried to use cordova-background-mode plugin in IOS with Cordova works but with the same code in Capacitor doesn't work

Capacitor App: https://github.com/nicobytes/capacitor-background
Cordova App: https://github.com/nicobytes/cordova-background

There is the branch https://github.com/ionic-team/capacitor/tree/wip-background-task but is inactive but has a method RegisterScript, that idea is possible?

Is there any native API for Background Tasks in IOS?

My context

What is the better approach for background task for a timer, I'm making an app for sleep routines with sounds when the user wants to start to sleep start a sequence.

I have a timer for each phase when the time is up should continue with the next phase automatically with a new sound.

@jcesarmobile
Copy link
Member

Issues tagged with feature request are closed but tracked for👍 reactions to gauge interest.

@ptmkenny
Copy link

I'm a bit confused. I arrived at this issue from the Capacitor 3 upgrade guide, which states "subscribe to #3032 for updates." Is this the correct issue?

@pete-mcwilliams
Copy link

I also arrived at this issue from the Capacitor 3 upgrade guide, I'm concerned that the plugin has been removed, I do not agree that this this feature request relates to the existing capacitor 2 functionality.

@fhrbek
Copy link

fhrbek commented Mar 22, 2021

Same here, I came here from the Capacitor 3 upgrade guide. Unfortunately, the removal of this plugin blocks me from upgrading unless I find a way how to do the same thing with a different, non-capacitor plugin.
I use the Background Task to schedule local notifications when the user quits the application, sends the application to the background, or switches the phone to standby mode. The task finishes in a few seconds so it perfectly matches my needs and I don't have to care about the background task limitations.
Is there anyone who could give me advice on how to replace this (from my perspective) perfectly working plugin that has been removed?
Thanks!

@jeffersonveloso
Copy link

I also need the plugin to work in the background for my app, my app is for inventory management and needs to receive events via websocket even in the background.

@robingenz
Copy link
Contributor

My app also uses the Background Task plugin. To be able to migrate to Capacitor 3, I've created a Background Task plugin for Capacitor 3. It has the same limitations as the Capacitor 2 plugin.
Feel free to use it: https://github.com/robingenz/capacitor-background-task

@pete-mcwilliams
Copy link

Very nice, would you consider adding it to the community plugins?

@robingenz
Copy link
Contributor

Yes

@rhyek
Copy link

rhyek commented Apr 14, 2021

@robingenz As someone who is completely new to capacitor, what are the limitations of your plugin? I see under quirks it mentions There is currently no ready implementation on Android. It's planned to add the support in the near future.. What does that mean in practical terms? Thanks.

@robingenz
Copy link
Contributor

@rhyek Welcome! This means that there is no Android support yet, even if no errors are thrown when calling the methods on Android. The API exists and can be called. However, the actual implementation is missing (see here).

@fhrbek
Copy link

fhrbek commented Apr 17, 2021

@robingenz it means that I cannot use it now (I want to support both iOS and Android). I'll postpone the capacitor upgrade then and, if I have some time in the future, I may try to help to add the android support (but not now, sorry, too many things to solve ATM, I'm feeling a bit overwhelmed).

@robingenz
Copy link
Contributor

As I said above, it has the same limitations as the Capacitor 2 plugin.
The Capacitor 2 plugin does not have a real Android implementation (see here) and neither does this one.
I created it to make migration easier and since I was planning to create a background task plugin with Android support anyway.
Let's address further questions in the plugin repo please.

@patrickmetz
Copy link

patrickmetz commented Jun 26, 2021

I just wanted to point out, as a side note, that there's a plugin
for scheduling repeated background fetch tasks.

It seems to be up-to-date, but has fairly limited functionality:
capacitor version
cordova version

EDIT: Both are free and distributed under MIT License
(Displayed at the bottom of their respective GitHub pages)

@pete-mcwilliams
Copy link

That's a good solution for the original issue

I don't think it helps those of us arriving at this issue from the link on Ionic's release notes for capacitor 3 where this issue is conflated with the removal of the Capacitor plugin (background task) which provided a little time to complete tasks on closing the app.

@5hee75
Copy link

5hee75 commented Aug 6, 2021

I'm coming from the v2 -> v3 upgrade guide, so I'll explore some of the options here. A real background plugin would be amazing, I think.

I've also submitted a SO question about how to do long-running uploads with Ionic and Capacitor, which is why I'm using the BackgroundTask plugin. Is this plugin even truly needed for that, or is there a different/better approach?

https://stackoverflow.com/questions/68675651/what-is-the-best-way-to-manage-long-running-uploads-with-ionic-and-capacitor

@Sibi4
Copy link

Sibi4 commented Oct 1, 2021

Hi @robingenz

My app also uses the Background Task plugin. To be able to migrate to Capacitor 3, I've created a Background Task plugin for Capacitor 3. It has the same limitations as the Capacitor 2 plugin. Feel free to use it: https://github.com/robingenz/capacitor-background-task

I installed and used it but my task doesn't work when I close the application, it works only when I minimize it (after pressing Home button for example).

App.addListener("appStateChange", async ({ isActive }) => {
  if (isActive) {
    return;
  }
  // The app state has been changed to inactive.
  // Start the background task by calling `beforeExit`.
  const taskId = await BackgroundTask.beforeExit(async () => {
    // Run your code...
    Notifications.showNotification();
    // Finish the background task as soon as everything is done.
    BackgroundTask.finish({ taskId });
  });
});

I want to send some local notifications even when the app is closed/removed from the multitasking bar. Could you please tell me what am I doing wrong?

@khilnani
Copy link

khilnani commented Oct 2, 2021

It would really be good to have this - without this building a whole category of apps (scheduling, meeting, timers, event-based apps) that leverage local notifications are not really possible

@Fabchick
Copy link

Performing background tasks would be great! We are currently working on an over-the-air update feature using bluetooth low energy. This process takes 5 minutes and should not be stopped when the app is not active anymore.

@jrochel
Copy link

jrochel commented Dec 20, 2021

I wanted to migrate from Cordova to Capacitor, but background tasks (specifically background upload) is crucial to my project.

@notflip
Copy link

notflip commented Jan 31, 2022

Does anyone know if there's a workaround at this time? I need to handle 5MB photo uploads (on Android), and I feel like I won't be able to do it without background tasks

@tonn
Copy link

tonn commented Jan 31, 2022

The biggest limitation is on the WebView side - when you minimize app all timers begin to emit with minimal 1 second interval. So all logic witch depends on setTimeout and setInterval (mostly all async logic) begins to work wrong. Also after 5 minutes all timers stop to emit. And this can not be fixed with js code or native plugins.

So if you really need to do something in the background - you need to write native code and work with native background limitations.

@sandstrom
Copy link
Contributor

@notflip 5 mb should be ~13 seconds on 3G (3 mbit). I'd just show a progress bar.

@notflip
Copy link

notflip commented Feb 1, 2022

@sandstrom That's what I'm going to do indeed, I'm going to see how low I can turn the Capacitor Camera quality property until it looks good.

@kevnk
Copy link

kevnk commented Jun 10, 2022

Do you know if Background Tasks are 100% necessary if I need to play audio natively when the phone is locked or when they leave the app?

@gldstrrbt
Copy link

Please integrate this feature. Currently in need of it to deliver local notifications after users close the application/on boot.

It's beyond a headache working with other solutions which don't have the best documentation or issues sections, especially when alert settings are kinda crucial for certain applications.

@robingenz
Copy link
Contributor

robingenz commented Sep 28, 2022

For anyone stumbling across this issue, there are currently the following Capacitor plugins that can be combined:

I will soon publish a blog post with a guide on how to best combine which plugins to run Background Tasks on Android and iOS.

@peitschie
Copy link

A few more plugins that have some helpful hints:

The cordova-plugin-run-in-background is especially useful as the disableWebViewOptimizations function there is a way of ensuring timers continue to fire reliably in the background even after the app is pushed to the background: https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background/src/master/#markdown-header-quirks

This works by sending a "VISIBLE" state to the WebView a few seconds after the app is pushed to the background, which seems to restart all the timers back to their normal interval. With this technique, I've been able to keep an app reliably executing in the background (at least until doze mode kicks in) on a variety of phones.

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 10, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests