Xamarin IOS background task on a schedule - ios

I am writing a Xamarin application that needs to download and upload data on a schedule.
Currently the task is working fine on Android by using the Android Foreground Service along with the Notification Manager. https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services
BUT in IOS...
I can't figure out how to get a background service to stay running on a schedule (every 10 minutes) when the application is pushed to the background or the screen is off.
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/
I can't use the Background Fetch because it runs when it feels like running and I need to run on a strict 10 minute schedule.
I can't use the Background Transfer Service because it only "starts" by the Background Fetch which is back to not running every 10 minutes.
I can't use the Remote Notifications because the app is doing the "push" to the service and not the other way around.
Since I am gathering GPS data, I tried to use the IOS GPS Location Manager Service but that only works if the device physically moves inside a 10 minute window and some of the devices do not move every 10 minutes YET we still want data from the device.
I tried to use a Background Task with the Expiration Handler but it dies after 5 minutes.
https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks
Any suggestions are greatly appreciated.

Related

Swift background work

I'm new to iOS development and I'm using Swift in my current project.
I'm trying to achieve something similar I did in Android. I'm trying to run a background service when the app is closed/minimised that will get executed every 2 seconds to keep the user informed that the app is still running in the background and checking for status with the server.
How can I achieve that? I've tried using the "background fetch" method but it doesn't get executed every 2 seconds.
The background service will run no longer than 5 minutes if it matters.
No you can't do that. What you can do is, call a service in applicationDidEnterBackground that lets your server know that your app is in background or killed(You will get very less time for this to get executed, approx. 3-5 sec.).
An Android app like the one you described will likely drain the device’s battery prematurely. This is the simple reason why Apple has restricted background execution to a clearly defined set of tasks:
Apps that play audible content to the user while in the background, such as a music player app
Apps that record audio content while in the background
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
This list is taken from Apple’s official description of background tasks: Background Execution.

iOS GPS Tracking app shutdown in background

I'm currently working on an iOS app using objective-C that send every minute gps coordinates to my API.
I've used AFNetworking & CoreLocation, I also wanted my App to run all day long, but only send coordinates if time is between 8am - 6pm.
Everything's working fine on iOS simulator targetting iOS9 my app is sending his location every 1 minute to my API, even if i press home or lock the phone.
PhoneCall, loss of network or GPS have been handled in order to keep my task running.
But when I tested it on some device (iPhone 4S on iOS9) Location stop updating after an average time of 1hour to 1hour and a half.
Is iOS shutting down my app in background after a given time?
If so, is there a way to keep my app doing her job in background for at least 10 hour, without having to prompt the device user?
Implementing long running background task are "allowed" by apple if it concern:
"The app keeps users informed of their location, even while it is running in the background."
Does that mean background task will block my http request after a while?
For long term deployment, I wish my app to be upload on the App Store, but I've read many post about app GPS tracking app which have been rejected.
Here's the minimal list of requirement my app should have:
Send GPS coordinates to my API every minute
Working in background for a minimum time of 10 hours without shutting down or prompting user to reload the view
Could those requirement be accepted to get my app upload on AppStore?
Or will I have to use local/remote notification and prompt the user to reload my app in order to keep it running for such a long time?
The documentation says.
The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.
I think this will be apt for your scenario.

How to tackle an app that fires recurring local notifications but on a changing schedule?

I'm creating an app that needs to fire local notifications for sunrise and sunset on a daily basis.
Sunrise and sunset times changes every day.
I want the registering of the local notifications to take place even if the user didn't open the app (i.e. a background task). So the user installs the app, sets his location (since sunrise and susnet calculations are location based) and then he knows he'll get notifications for sunrise and set every day without needing to open the app or being connected to the internet.
Ideally I need a background task for registering local notifications over a given period which runs at least once every period. "e.g. registers local notifications for the next week, which in that case I need it to run at least once a week".
None of the current background modes fit my case, I tried a background fetch, but since there're no data downloaded iOS stopped running my background task after a couple of days
I know I can use push notifications, but I don't want to unless it's my last option, cause I want the notification to take place even if the phone was not connected to the internet.
So how do you think I can tackle this? Is there something I'm missing?
Get Location for every ’n’ minutes in background. By Using beginBackgroundTaskWithExpirationHandler and NSTimer .it keeps app live in background.
So, You can schedule a localnotification in background.
For getting location update in background Refer the link
How do I get a background location update every n minutes in my iOS application?
(From iOS7 finite-length background task time reduced to 180 seconds. so you must fetch location within next 180 seconds.)
If user force quit the app. the app destroyed all the task . Use Significant location change or region monitoring to restart the Background task and get location for every ’n’ minutes.

Frequent background location updates in Swift

I am creating an app that's using a CLLocationManager to track my position and update it to a database with a Google Map. The sole purpose for this app is to stay in the background and continue to update your location as long as you've logged in and set an Available key to true.
The app works fine in the foreground and it updates my location every minute, but when it's in the background it stops after 15 minutes as the app suspends (I suspect). I've implemented the startMonitoringSignificantLocationChanges() method, but if I stay in one place for too long without opening the app manually after 15 minutes, the updates stops and my map only plots your location for so long without you updating and resetting the lastLogged timestamp.
Is there any way I can force-activate the app to a background state without actually opening the app itself? As the plan is for many people to start using this app, opening the app every 15 minutes is going to be a massive bother and I would love for some way to do this automatically.
Thanks for any help!
PushKit allow you to send messages directly to the people who have installed your app, even when the app is closed on a device or in background.
Its a new framework in iOS 8, is the new push notification mechanism which silently launch your app into the background without the need of user interact with the app.
Here is link in Apple:-
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/PushKit_Framework/

Before iOS7 was it possible to execute background task if it didnt involve audio, location etc

Apple has provided certain services which can run in the background for 10 minutes, but what if I have to perform some other task like downloading a file ... how much time limit do I get for it
Before iOS 7 you could request up to 10 minutes of background time (via beginBackgroundTaskWithExpirationHandler:) or you could use any of the background modes available at the time (such as location, voip, etc).
VoIP handler, for instance, will be called at most every 10 minutes and will give up to 3 minutes of background time IIRC.
With iOS 7 you can download and upload files out-of-process, without your app running. Please check documentation on NSURLSession and NSURLSessionConfiguration.
You can do it (directly after your app went to the background, that is, not let's say an hour later), and you approx. do have 10 minutes, although that is not guaranteed. See here in the apple documentation:
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20
You can request a Executing a Finite-Length Task in the Background
which will run in background for maximum 10 min. This should be long enough to download file.
So no it is not just for certain services, you can get your app running the background for a longer period of time if your app is a VOIP app, track user location, playing audio or your app is app for an external accessories.

Resources