I'm looking to perform network tests every minute for an hour to check whether or not the user has a connection -
Ideally I'd like to get their location every minute (but not 100% necessary) and ping a server. Is this possible by either the user pressing a UIButton "start test" and then if they close the app it still runs?
Or, activate a function when the user is in a location range at a given time? (Looking to perform tests at events)
Or, possibly using a push notification to activate the function? This seems very impractical though.
My research has shown this is typically only available for VOIP or Audio apps, and background fetch based are either restricted in duration or scheduled by the device itself based on the user's behaviour, which I don't want.
Note: The app doesn't necessarily have to be released on the app store, this is just a small project so running it on a few devices will suffice.
Related
Preamble
I wrote a mobile application which should show upcoming events. The app downloads it's data from server. Data is prepared in batches once every 24 hours and it's ready to be fetched after 4 am. This gives me a perfect opportunity to sync it overnight and to make new data immediately available when the user opens the app.
Background fetch
This was my first approach for syncing data with the server. It's advertised as very powerful feature, but using it alone (out of the test environment) is not enough:
There is no way to force background fetches to execute at exact intervals.
I thought that the frequency of background fetch operations could be configured
// Setup background fetch
let timeIntervalEveryHour: NSTimeInterval = 3600
let sharedApp = UIApplication.sharedApplication()
sharedApp.setMinimumBackgroundFetchInterval(timeIntervalEveryHour)
but it's still dynamic and I think it is never for users who did't use app very often.
If 'Background App Refresh' is disabled, which automatically happen if device is in 'Low power mode', background fetch won't be triggered.
Other problems like Data Protection when device is locked and 30 seconds window for completion of all tasks are considered.
Remote (silent) notifications
So I took the next step and configured the server to post silent notifications once the batch is ready just to found that this is also not enough:
If application is force killed by the user or device is rebooted, notification won't be handled.
Rate limits. Delivery will be delayed, this depends on a variety of factors that are not explicitly specified by Apple, but probably - battery life, whether phone is on cellular, etc.
Sometimes silent push notifications are dispatched when application starts, which could lead to race conditions with the check for manual synchronisation. So I'll try to force it by adding "alert" = ""; to payload. (As it is suggested here)
Silent Push notifications could be disabled by the user be setting off 'Background App Refresh' - source
Manual synchronisation
To be sure that data is always up to date, if it's not recently updated, when app comes to foreground user is presented with alert which asks for manual synchronisation. Also it could be started later from settings tab. Unfortunately, according to analytics, most of the fetch requests are made manually.
First run case is also handled.
Next steps
I'm considering using VOIP notification. They should wake the app even if it's force killed. However I'm concerned that that would cause app get rejected.
Questions
Is there something I'm missing? I know that background synchronisation depends on a variety of factors, there could be no internet connection and etc, but is there any way to make it more reliable?
My iOS app (which targets iOS 8.1+) use location services to determine if a user has entered a particular region during an event. Ideally I would like to enable the geofencing a little before the event and turn it off a little after the event completes. The problem is there is no guarantee the app is running an hour before the event so I turn on geofencing at the point the user registers for the event. This is not the best approach as it means the geofencing is on for much longer than it needs to be.
As far as I can tell, there is no way to "wake up" the app in the background at a scheduled time in iOS. I could use the push notification meant for updating content, but It's not clear to me if Apple would reject such a misuse of that notification.
Any suggestions?
I've been looking into this myself.
If you can arrange a push notification from your backend ("Silent push notification", aka "content-available"), it seems to be a good option. In real-world situations, this seems to give you the best control over the timing. Unfortunately, it won't work if the person doesn't have connectivity. Also, unfortunately, you need a backend that can queue events at a time (not just in response to an input.) If you have such a backend already, and your app is only useful to the user when they have network coverage anyway, this is probably the best option. It seems this an appropriate use of the technology, so Apple should approve.
Another option I'm trying is to use background fetch. You specify a "minimum interval" to avoid too much fetching. Try 50% of the remaining time-to-event as a minimum. Every time the app wakes up (whether in the foreground because the user opened it, or in the background because background-fetch opens it) you can calculate the time-to-next-event, update the fetch interval, or start the region monitoring. You are supposed to use "background fetch" to fetch information from a server, but there doesn't seem to be any requirement to poll a server, you could poll your internal data instead. I haven't fully tested this yet but it seems promising.
You can use significant location change monitoring, which I've read will wake up your app briefly every 15 minutes or less, and you can use the time/location information to decide whether to turn on the geofence. I think this would work well in combination with the above "background fetch": Many hours or days before the event you rely on background fetch, which you then use to turn on significant location change monitoring a few hours before the event. (There's speculation that geofencing is actually more battery efficient than significant change monitoring, but you could choose to assume that other apps on the user's device will already by watching for significant changes, in which case the marginal cost of your app adding itself to the list should be minimal.)
Putting them all together, you could create a sequence of
background fetch -> significant location monitoring -> geofencing
as the time gets closer.
There is also the CLVisit monitoring functionality, it's not understood very well, but supposedly uses less power and is called less frequently than significant location change monitoring. If the background fetch or silent remote notifications aren't working to wake up your app, give this a try, and please report back!
You can't (yet) do a silent content-available local notification (AFAICT). However, perhaps you can schedule up a local notification "Your event starts tomorrow" or something that convinces the user to click the option that starts the geofence. Here's a tutorial on it http://www.appcoda.com/local-notifications-ios8/, the response of action can be UIUserNotificationActivationMode.Background so your geofence can come on (if the user responds to the notification) without bringing the app to the foreground.
It's been 5 to 6 weeks since you asked, do you have your own answer already? Please let me know.
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.
I have made an application which needs to send Location and status update to server every 1 minute.
I tried below ways but none of them helped me out. is there any solution for this?
1 - NSTimer - Many people suggested to do this way. but the problem is going to backgroundMode and it only works for 20 minutes. after that application stops sending data.
2 - BackgroundFetchMode - at beginning looks like the correct solution. But this ability do not guarantee to run application at every 1 minute. it has an algorithm that iOS decide which application should run.
This API is not like a timer task, system will decide when to call the
handler depending on many constraints.so if you set timeInterval to
2.00f(2secs), handler is called for every 2+(minimum) secs.
3 - LocationUpdate - again this way do not works because it only run your application if you move at least 500 meter.
Apps can expect a notification as soon as the device moves 500 meters
or more from its previous notification. It should not expect
notifications more frequently than once every five minutes. If the
device is able to retrieve data from the network, the location manager
is much more likely to deliver notifications in a timely manner.
4 - Push Notifications - in an article said in this method you can run an application within defined time and it really does !
you can schedule a notification within defined schedule time. but the problem is showing notification to user. which I need something silent in Background.
Also , you can not run some code every 1 minute. it just show a notification to user. and user should tap on your notification and then didReceiveLocalNotification
can be available and you can run the code.
As Apple states in their documentation:
In iOS, only specific app types are allowed to run in the background:
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
Apps that implement these services must declare the services they
support and use system frameworks to implement the relevant aspects of
those services. Declaring the services lets the system know which
services you use, but in some cases it is the system frameworks that
actually prevent your application from being suspended.
You can read all about implementing and declaring those background tasks in the link. However, if yours is another type of App, or you cannot use the system frameworks, there is no way for your App to run in the background indefinitely. And even if it is, you should always expect that the system stops your task for some reason (f.e. restarting the phone).
I'm writing an app for a client where the users will enter data during the day, then at a given point at night (say, at midnight) the user's data for that day should upload to the server.
Therefore I need to schedule a method to run on my app at midnight, even if the app is in the background. This method will then sync the collected data with the server and download any changes.
In order to achieve this, I imagine I need to set the app to always run in the background (i.e. longer than 10 minutes) then schedule the function to run after a specific time. Do I do this by using performSelector: withDelay:? Or do I need something more robust because the app will be in the background?
Thanks guys!
You might check out UILocalNotifications. You could schedule that event to happen and then set a badge icon to let the user know it processed something (or not).
Info on these HERE
I don't think your app or what you are trying to do is qualified for Apple's requirement for long running background task. Check out the "Implementing long-running background tasks" section of this doc from Apple.