.NET MAUI Recurrent Background Job that runs few times a day - android

I would like to have a background job to run like 2-3 times a day, but even less it's ok. It's just a quick api call to my server, it doesn't need to update the UI, infact I prefer that it runs when the app is not in the foreground, but that's another topic.
I've read that latests versions of Android and iOS or even manifacturers restrict the app but I don't need these task to be at certain time precisely.
I'm not sure if I should use the JobScheduler or WorkManager for Android and the BackgroundFetch or BackgroundTask for iOS.
Let's say the task should run 3 times a day so every 8 hours roughly.
The thing is that a user might not open the app again so the task should be scheduled to be recurrent, maybe every time it runs it could schedule another one, but if one fails that line could never be reached.
I don't need to support old operating system version, only iOS 15+ and Android api 30+.
Thank you for the help!

Apple is strict about hardware resources occupied by App, not to mention App in the background state. When App enter the background state, it will soon be suspended by the OS, unless you apply for permission from OS. Only several modes could be allowed to run in the background, see the following picture
You want a background cron job which is not allowed in iOS.
For Andriod, you know we could use WorkManager or JobSchedule. Here are some documents that you could refer to:
Getting Started With WorkManager
Android Job Scheduler
Hope it works for you.

Related

What's the proper way to schedule tasks and reminders in Oreo?

Background
Ever since Android O came out I had trouble with what classes and methods I should use when I want to schedule background sync tasks and notifications, more specifically, I want to do these two separate things:
Background sync task to download data from the server, and notify the user about important information (that was just fetched from the server). These syncs should occur at an almost precise time of the day (e.g. 21:30 ±10min).
Notify the user about an upcoming event, for example, one week before an exam.
Because Android O has placed some restrictions on AlarmManager, I cannot set a background service that runs at a specific time of the day, unless I use getForegroundService(), which, as the docs say, should only be used for services that are noticeable to the user.
What I tried / considered
I have been using JobService that runs periodically every so and so hours, but I would prefer for it to run at a more specific time of the day.
I have looked into CalendarProvider, and also considered a push notification service, but it seems to me like an overkill for simple tasks like these.
Question
My final question is what methods I could, or I should use to implement the above features?
Use WorkManager this is the best way to Run Task in Background in Android Oreo and Pie versions and it also works in older versions of Android.
See the Documentation
https://developer.android.com/topic/libraries/architecture/workmanager
https://developer.android.com/topic/libraries/architecture/workmanager/basics
https://developer.android.com/topic/libraries/architecture/workmanager/advanced
GitHub Example
https://github.com/krunalpatel3/WorkManager-Example-Andorid
Reference
http://thetechnocafe.com/how-to-use-workmanager-in-android/
https://www.youtube.com/watch?v=fAQKvBHeg_w
https://www.youtube.com/watch?v=1VVir3-4hII
https://www.youtube.com/watch?v=0jgkQYebYvQ
https://www.youtube.com/watch?v=ooP8kkhvRQI

Android Beacon Library - Manually starting background scan

This is a question regarding Android Beacon Library.
Because of Android 8 (Oreo)'s restriction on background service, the author of the library has changed the mechanism of starting background service in Android 8.
As far as I can observe, the background service needs a much longer time to start than Android 7 or below, even if the app is already started.
What I want to do is to reduce this time as much as possible.
One of the way is to force launch the background scanning if the app is opened by the user.
For instance, I want to do something like
BeaconManager.getInstanceForApplication(this).startbackgroundScanningIfNotRunning();
In onCreate() of MainActivity.
But I think there is no such API. Is there anyway to do this?
Or, is it possible for the library to handle itself (through an app update)?
While it is possible to do something like described in the question, it may not be useful. The problem is that Android 8 prevents apps from running in the background long-term. In order to manually start a scan in the background, your app must be running in the first place. If Android 8 has disallowed this, then you aren't going to be able to do it, anyway.
For an explanation of how Android 8 blocks apps from running in the background and how the library works around this, you can read this blog post if you have not already:
http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8
If you have somehow managed to get your app running long-term in the background (e.g. by using a foreground service), you can always kick off a scan manually by starting beacon ranging/monitoring in foreground mode (from the library perspective), which will schedule an immediate ScanJob that will keep running and start itself over and over. To make sure you are in foreground mode, you simply need to refrain from using the BackgroundPowerSaver and don't make any calls to beaconManager.setBackgroundMode(...) to true.

Running service to download content (SyncAdapter vs JobScheduler vs Service)

I am still new to android and working on an application that works on Android (API >= 21). This application displays data that is previously downloaded from a server on the local network so I need to implement a service to download the content, on-demand & Periodically, on the device.
SyncAdapter
I've implemented this "Downloader" using SyncAdapter and it was working fine but at the end the code was really verbose:
The application does not have ContentProvider. The content is downloaded as files.
It runs on a local closed network so there is no need for authentication.
The application had 3/4 extra classes that not doing any real job.
JobScheduler
After some reading and searching, I decided to go with JobScheduler.
"JobScheduler is guaranteed to get your job done." medium article
It looks easy and has clear API, I said, so I re-implemented my "Downloader" with JobScheduler and the result was really good. The application was able to download the content, writing a good log to trace errors and operations. The Job runs when the device is Idle and kicks off/stopped, on demand, as expected.
I used alarm manager to implement the periodical calls of the job and turning wifi on. Oh, I forgot to mention that the application is the responsible of turning on the wifi because it is inside a case and works as Kiosk
The problem is that there is a catch. This is not mentioned in the documentation, or I was blind not to see it, at all. The Job is limited to 1 minute only (1 minute on lolipop and more on Android >= 0) then the onStopJob() will be called. So my is cancelled before completing the download when the data is a little big. Of course I can keep my thread going and continue download in the background but in this case I can't benefit from the API to maintain a good log, reschedule my job and manage Wifi status.
I need another implementation
I need something similar to SyncAdapter or JobScheduler that runs when the Wifi is on and when the device is Idle.
I am not sure whether triggering a service from JobScheduler is the solution I am left with. I need a little certain information before implementing the same thing for the third time.
Any idea?

Is there any equivalent of Services of Android in iOS?

I want to check the database in my app every 12 hrs for rows with date column having corresponding date. This is accomplished by writing service in android. But is there any equivalent of services in iOS so that my requirement can be accomplished?
No. There is no such thing in the SDK or in iPhone/iPad in general. You can only write code that will affect the eco system of the app, not the operating system.
When your app is closed it's closed and no action will be taken until the user opens it/opens a push notification related to your app.
If the user approved location based services for your app, there are a few ways to run short background process even if your app is totally closed. One of them is by using Monitoring Shape-Based Regions which basically means if the user left region X/entered region Y
open the app and run a few commands before closing it again.
The clever way (and the only way I can think of) to accomplish what you're after in iOS is to run that service on a server and pull the data from the server when the app is opened.
In iOS7 and later you can use background fetch for this task.
You can check this tutorial:
http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520
iOS Background work
Nowadays you you are able to use Background fetch or Background Processing Task or URLSessionDownload/UploadTask for doing something when app is in background more
[Xcode Background Modes]
[Background session]
You can find the solution here. Background Execution does this.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Added: Apple does not allow apps to run in background for all the time. It provides some finite-length time to complete your app execution. You can increase that time depending on your execution need. But that is not recommended.

What is relevant to android.app.Service in WindowPhone 7

I'm trying to make a stopwatch & countdown app for WindowPhone 7 using Silverlight for WindowPhone SDK and trying to make it run in background when it's tombstoned. In Android, I can use android.app.Service to run it in background. According to MS AppHub Quickstart, "The Windows Phone operating system doesn't allow any third-party applications to run in the background". Please help me if you have any idea for keeping the countdown running when a phone call is received or the phone goes to sleep. Thank you.
At the moment, once your app is tombstoned, your app cannot continue with any custom processes such as having your countdown continue. The Mango SDK coming out this month allows for a bit more freedom in this respect.
If, however, you want to run your app under a lock screen, this is possible. You can simply do this:
PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
Jaime Rodriguez has a great post on running under a lock screen which you can read here.
Until Mango comes out, there's no way to have your program run in the background.
As keyboardP notes, you can't continue your process on the phone.
However you could use a server and Toast message popups to still notify the user. It requires more setup and a server to run against, but it will meet your requirements.
One thing I have seen with other people making apps like this is to store the time that app was tombstoned and then, when the app is reactivated to look at the difference between the current time and the saved time and deduct that from the countdown.
This may or may not be appropriate, depending on your requirements but may be something to consider.

Categories

Resources