I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.
I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.
Let me explain in details what do I want my application to do.
Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.
Could someone propose a solution how to implement this idea in iOS app?
On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).
I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.
There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs
The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:
Silent notifications are not meant as a way to keep your app awake in the background, nor are they meant for high priority updates. APNs treats silent notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.
Silent pushes are described in Apple's push notification docs.
iOS Background Execution Limits
Questions
How do I keep my app running continuously in the background?
If I schedule a timer, how do I get it to fire when the screen is locked?
How do I run code in the background every 15 minutes?
How do I set up a network server that runs in the background?
How can my app provide an IPC service to another one of my app while it’s in the background?
Answer from Apple:
The short answer to all of these is You can’t. iOS puts strict limits on background execution. Its default behavior is to suspend your app shortly after the user has moved it to the background; this suspension prevents the process from running any code.
Official: https://developer.apple.com/forums/thread/685525
Related
I need my app to wake up every x hours, fetch the data from device and send it to the server.
Is it something doable? If I can implement voip pushkit or fcm?
Thanks.
Use background task
Request the system to launch your app in the background to run tasks.
Thiese articeles might help you.
Background Tasks
BGTaskScheduler
A sample app from Apple.
Refreshing and Maintaining Your App Using Background Tasks
A WWWDC toturial
Advances in App Background Execution
You cannot do it periodically, but you can do it using VOIP Pushkit, through help of server. But I am not sure if apple allows VOIP without calling feature.
This can somewhat be achieved by VoIP push notifications using PushKit, VoIP push notifications don't appear on a device with no sounds and badges, so one can produce this after every interval and do respective actions on it.
Refer to this link:
https://medium.com/mindful-engineering/voice-over-internet-protocol-voip-801ee15c3722
OR
What you can do is before your application goes in the background or terminated you can collect the data and send it to the server. This can be done with this(refer to the code) :
https://www.hackingwithswift.com/example-code/system/how-to-run-code-when-your-app-is-terminated
Remember you get only 180sec to do this background task (as per my research).
You can initialize this as
application.setMinimumBackgroundFetchInterval(1800).
Every time your application is brought in the background or terminated this function will get a call.
I am building an iOS application in SWIFT where user location is fetched and sent to the server after every 30 mins. When the app is opened it keeps sending the location lat/long to the server but when the app is closed or suspended it won't send. I want to send the location even the app is closed. Is this somehow possible? I just want to send the location to server no need to open the app again and again?
You app needs to support Background Tasks for being able to run in background.
I believe this has been answered multiple times. Here is the documentation link
Since you are using location, I would recommend you to use UIBackgroundModes as locations.But be sure you don't drain the users battery.
Also usage of background fetch (UIBackgroundModes - fetch) and silent notification can be a great way to get you app running after equal interval of time.
Update
For performing operations you can use silent notification - refer this apple documentation
As per documentation - the system wakes your app in the background and calls the application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
Update 2
For local notification along with scheduling custom action refer this apple documentation. But I really doubt this will help you achieve your desired result. Mainly because you app would get an event only when the user performs any action on your local notification and if he ignores it, you app will remain in background.
As stated clearly in Apple docs, beacon ranging can be done in background for a short period of time only, say up to 10 seconds by default or up to 3 minutes with the help of background task expiration handler. As per my app's use case, app needs to do beacon ranging for every 15 mins until user exits the region. I am thinking of using background push notification(silent push notification) for this purpose(assuming data connection is available on the device always). So the flow goes like this, upon user entering the region, app calls the server with device token, server sends silent push notification for every 15 minutes. Once app received push notification, it does beacon ranging within allowed period of time if needed. Question I have here is whether using push notification in background mode to do ranging is legal, will I face any issues during app store submission.
Note: Also I need to enable BLE background mode for the app, to read some characteristics from some BLE devices.
Technically you can do it, but Apple mostly rejects such app. One important thing you have to consider is that, if the app is manually killed by the user and not running in the background, then the app won't wake up with silent push notification. There is a workaround if you have VoIP push notifications it will wake the app even from the terminated state. But you might need strong reason while pushing it to AppStore.
If you misuse one of the background modes, the app will probably be rejected, saying that, I don't think silent push notifications were meant for: keep an iOS app in "Background" state by sending it a silent push notification every few minutes.
another thing is that silent push notifications are rate limited as described http://asciiwwdc.com/search?q=push+notification, so I'm not sure if they will be sent every few minutes.
Apple says that;
Silent notifications are not meant as a way to keep your app awake in
the background, nor are they meant for high priority updates. APNs
treats silent notifications as low priority and may throttle their
delivery altogether if the total number becomes excessive. The actual
limits are dynamic and can change based on conditions, but try not to
send more than a few notifications per hour.
You might want to see this article. The user talks about apps that use silent notification for triggering location tracking. But eventually it's a hack that Apple may reject some time in the future, so it's best to have a contingency plan. FWIW so far I haven't heard anyone reporting rejection.
So the official answer is don't do it, as for the why you can refer to Ashish's answer. The unofficial answer is if you can't change your business logic then do it at your own risk.
The iOS application I'm working on does the exact same thing with the exception that I'm using recording instead of a Beacon. Recording by iOS standards gives more issues in pushing the app to the app store.
But Apple did not reject this app. Although we still are facing some issues but they don't relate to your problem.
You can follow such a tutorial for further help apart from the answer you were looking for : iOS Push Notification Demysitfied
Also, I've done firing of local notifications, while the application is in the background. BLE even works if the app is killed by the system, when the OS receives some communication from your peripheral or central, iOS wakes your app up and executes the desired function, before putting your app back to sleep.
I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.
I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.
Let me explain in details what do I want my application to do.
Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.
Could someone propose a solution how to implement this idea in iOS app?
On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).
I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.
There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs
The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:
Silent notifications are not meant as a way to keep your app awake in the background, nor are they meant for high priority updates. APNs treats silent notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.
Silent pushes are described in Apple's push notification docs.
iOS Background Execution Limits
Questions
How do I keep my app running continuously in the background?
If I schedule a timer, how do I get it to fire when the screen is locked?
How do I run code in the background every 15 minutes?
How do I set up a network server that runs in the background?
How can my app provide an IPC service to another one of my app while it’s in the background?
Answer from Apple:
The short answer to all of these is You can’t. iOS puts strict limits on background execution. Its default behavior is to suspend your app shortly after the user has moved it to the background; this suspension prevents the process from running any code.
Official: https://developer.apple.com/forums/thread/685525
I have an app that fetches data from server via json/http (actually, it synconizes data with Core Data). User needs to be notified when new data is available in 5-10 minutes. I am planning to have about 10 users (that as an internal enterprise app), so I can simply fetch data in background using NSURLSession.
But iOS may kill my app in case of low memory (or even user may kill it!), and no notifications would be delivered! So, I believe APN may be used. But this solution looks very complex for such a simple task and small number of users.
Ideally, I should have some service/daemon or (like IntentService on android), but I can't have it on iOS, so there are only 2 ways: APN or background app (which may be killed). What is the right way here?
Even if your app killed, your app can be opened in background and fetch data. This feature is called as background app refresh. After updating data, you can show user a local notification, which makes it easy by not using APNS. However, if you want to use APNS, you can use Amazon SNS to send push notifications. If i am not remembering wrong, it gives first million push notifications as free for every month. It is very easy to implement, though.