Can you describe more as to what you mean here.
Sorry for my bad English.
I mean please support easy way to help developer implement Background Task in app. Thank!
Do you have an example of the APIs on each platform you are looking to have abstracted?
Example I want to send a GET method to get news every 15 min without open app!
Andriod new component for this is the Android WorkManager api - doesn't seem like Xamarin supports this yet. But it would be great to have a scheduler for both platforms supported on Essentials
I strongly support this proposal. We needed this for several apps and always rolled our own implementation, which always added overhead until you got everything right. Scheduling actions is very platform specific, and there is a huge potential for miususe. I.E. iOS, if you take a look at the docs you can see that for apps that need to update from time to time as i guess @nguyenthanhliemfc has to do you can see that it states
Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches.
The only apps that can permanently run in the background on ios are voip, location and audio.
Android is a lot more liberal with its Job scheduler (docs). It can specify a deadline, indicate that the job is to be persisted across reboots etc. It is available from Android 5.0 onward, for 4.4 we would have to look at the Alarm Service. This is also more permissive than iOS, thus we would still be limited by iOS.
Finally, UWP is also mightier that iOS in that regard (docs). You have certain restrictions such as the minimum interval allowed between internally triggered background events is 15 minutes, and that your app only has 30 seconds to process your tasks.
In conclusion, the only common api I would feel comfortable to expose without overpromising is something like this:
public static class Scheduler
{
public static void ScheduleJob(JobDelegate callback, JobOptions interval);
}
where JobOptions is a wrapper class to manage the android and uwp specific parts as well as an ID tag to differntiate betwenn jobs. The Jobdelegate is responsible for deciding whether the task was a success and whether it needs to be rescheduled. Then document that iOS is way more restrictive.
This was exactly what we needed, invalidate and refresh a local cache periodically. We did not care when or if it happened, only that if it happened it was when the app was not in foreground but had been launched somewhat recently. This fits the cross platform api service we would expose.
In my company have Server and Client app. My boss assigns the job to the employee on Server
Case 1: Client need get new data from Server every 15 min if have new records Client app will show local notification about new records.
Case 2: Client app will send current location of Staffs to server every 15 min ( Maybe every 5 min or differrent time).
Client app run on tablet Windows (some Windows Phone), Android and iPhone
I'll add a +1 and a use case to this.
One example that comes up a lot is long-running tasks. For example, we had a phonebook app that, because of Reasons(tm) needed to do a large fetch of the latest phonebook on startup. Being able to reliably do this in the background would have been excellent. As it was, we had to do it in the foreground, and hope we were lucky enough that the user didn't switch away during the download.
So perhaps there are two cases here:
+1 Vote
+1
I saw this: https://github.com/aritchie/jobs
This is a good idea, I just need this functionality.
In my application, I need to periodically do synchronization with the server, so as in the project that showed @mattleibow, it would be nice to do Backround Task that are inherited from the interface. We can place or delete them from the container of active tasks, and thus get control over the tasks.
for example, when an application goes into sleep mode or is terminated, we can put the necessary task in the container. and when the application returns to its working state, take it back.
One moment is Apple's permission. They have a list of allowed actions in Background mode. We need to make sure that Apple will allow the publication of the application.
I know examples when developers indicate permission to play music in the Background mode, and download the data themselves. as a result, Apple does not allow the publication of such applications.
Relevant link for anyone looking for more info on this
Thank you @mzhukovs It's great topic. I still need a support from Xamarin Team release a simple way to implement some type of background task!
Beside this link is another way to implement background task LINK
+1
While this is in our backlog for a feature request I would recommend checking out the excellent Shiny project: https://github.com/shinyorg/shiny
@jamesmontemagno ...background task is all ok. ...HOW TO USE XAMARIN ESSENTIALS IN FOREGROUND OR BACKGROUND SERVICE IN ANDROID WHICH NEEDS TO RUN EVEN HEN APP UI CLOSES? How can we init the Xamarin.Essentials.Platform.Init? OR is there some workaround? Android background or foreground service doesn't have needed params to initialize that xamarin essentials dll. THIS BLOCKS ALL FEATURES OF XAMARIN ESSENTIALS TO BE USED IN A BACKGROUND OR FOREGROUND SERVICE. NOTE: service is backbone architecture of worthy android apps, rest is all for amateurs.
TO BE SPECIFIC:
This code cannot be called in Service class as these params are not available in a foreground long-running service which doesn't close after app closes.
// Initialize xamarin essentials
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
@jamesmontemagno ...background task is all ok. ...HOW TO USE XAMARIN ESSENTIALS IN FOREGROUND OR BACKGROUND SERVICE IN ANDROID WHICH NEEDS TO RUN EVEN HEN APP UI CLOSES? How can we init the Xamarin.Essentials.Platform.Init? OR is there some workaround? Android background or foreground service doesn't have needed params to initialize that xamarin essentials dll. THIS BLOCKS ALL FEATURES OF XAMARIN ESSENTIALS TO BE USED IN A BACKGROUND OR FOREGROUND SERVICE. NOTE: service is backbone architecture of worthy android apps, rest is all for amateurs.
TO BE SPECIFIC:
This code cannot be called in Service class as these params are not available in a foreground long-running service which doesn't close after app closes.
// Initialize xamarin essentials
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Do you have try MessageCenter to call it from Service class?
Most helpful comment
I saw this: https://github.com/aritchie/jobs