ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.31k stars 962 forks source link

feat: Really background tasks #3032

Closed nicobytes closed 4 years ago

nicobytes commented 4 years ago

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 commented 4 years ago

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

ptmkenny commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

robingenz commented 3 years ago

Yes

rhyek commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

notflip commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

notflip commented 2 years ago

@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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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[bot] commented 1 year ago

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.