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.
Related
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'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
In my app, I want to check my database every 12 hours for an entry and if found, set a notification.In android,it is accomplished by using a service.But in ios services are not allowed.I tried to implement NSTimer,but it will be reset when the app goes to background.I want my service to be run when the app is on background. On research the only possible way I found is to use push notification.But if the network is disconnected,push notification will not work and notification will not be set that day.Is there any other possible way to implement my requirement please?
Long-running background processes are only allowed for VOIP applications that need to maintain an open connection to a server.
Push notifications are not guaranteed to be delivered, however, if your server sends your iOS device a push notification, the notification will be queued at the APNS server residing at Apple. When the target device is reconnected to APNS, the notification will be delivered.
The only way you can check the database is when your app is running. You can't do it when your app is on background.
Only VOIP, Music and Location based applications can run on background for long durations.
You can use UILocalNotification to alert the user to open your app.
I see iOS applications like Facebook that send notifications even if the application hasn't been launched.
I need to schedule a task with a timer that gets executed at every time interval, and this task (should be inside the same application) should deliver a NSUserNotification if meets some conditions.
How can you set up such a notification even if the application isn't running?
Facebook app and other apps that send notifications when they were not launched do this by using the remote notification mechanism.
To use this, you need to setup a server, create push certificates and use a APNS library for your preferred server.
Take a look here for details: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
Scheduled notifications can be done with local notifications, using UILocalNotification. However, like push notifications, local notifications cannot run code. They present a notification to the user, who may or may not choose to start your app in response.
If you want to run code in your app on a timed schedule, then your app has to run in the background. And Apple limits background apps to only a few specific purposes. If your app doesn't fall in one of those categories your app can't run in the background (see UIBackgroundModes key).
Significant location change monitoring can start you app when a location change has occurred but that is location based not time based and cannot be scheduled.
It cannot be done in the way you think, you (like Facebook) should use Apple Push Notification Service.