I'm making an alarm clock-like app. The user should be notified with a vibration and a sound every 10-60 seconds over a period of about 30 minutes. What is the best approach for this? It's easy enough doing it in the foreground, but how can i continue to run scheduled code in the background? I could use local notifications, but the user doesn't receive them when in "do not disturb" mode.
You should review the documentation about background execution: Background Execution and Multitasking. There are multiple ways of consistently running code in the background, but your app has to meet certain guidelines to use them--in your case, I don't think your app neatly falls into any of the categories Apple describes in Implementing Long-Running Background Tasks. There's always the possibility the submission reviews could approve your app anyway.
To avoid potential app rejection, you should implement your app using local notifications, and include a warning in your app that users should disable "Do Not Disturb" for it to function correctly. Unfortunately, there isn't a way for users to exclude apps from DND, nor is there a public API to manipulate DND.
Related
I want my app to be able to run some code every 5 minutes or so when it's inactive/suspended and the user is doing other stuff on the phone. Is there a way to accomplish this?
There is a way you can run code in background, although I would not recommend doing so every 5 minutes:
If your app’s server-based content changes infrequently or at
irregular intervals, you can use background notifications to notify
your app when new content becomes available. A background notification
is a remote notification that doesn’t display an alert, play a sound,
or badge your app’s icon. It wakes your app in the background and
gives it time to initiate downloads from your server and update its
content.
Essentially, you can send push notification that would look like the sample here:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
The content-available flag lets the push notification wake the app up in the background.
You cannot include any alert field or anything that would indicate it should be a visible notification or else this flag would essentially be ignored from my experience.
You would then handle this here: application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
Basically got this whole answer straight from the docs here: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app
the system may throttle the delivery of background notifications if
the total number becomes excessive
The above quote is why I wouldn't recommend doing it every 5 minutes.
I don't know exactly what you are trying to accomplish so I do not know if this would suit your needs or if this is the optimal answer for you without more details.
In a word, no.
Apple is very strict about when and where it lets apps run in the background. Normally you get told when you are being swapped to inactive, and can request background time to finish what you are doing. The time you get is 3 minutes at most, if memory serves. After that you get suspended (in memory but not receiving processor time) and can get terminated at any time without warning once you're suspended.
Apple does allow a tiny subset of apps to run in the background indefinitely, but is very strict about which apps qualify. Turn-by-turn navigation apps, music apps, and VOIP style communications apps are the only categories I remember.
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
I am building an app for a client that requires the ability for users to set an alarm time in the app, then be able to run the app in the background, then when the alarm time is hit display a notification.
I know Apple has some limitations on how long apps can run in the background, so what is the proper method for going about this?
I already know how to use local notifications, so showing a notification when the alarm is actually hit is not an issue. I just want to make sure I am able to enable the app to 'wake up' when the alarm time is hit and call the code for showing the local notification.
I don't believe that your app can wake itself up upon a notification, users wake your app up by interacting with the said notification.
However, you can keep your app running in the background by various methods so you don't have to worry about waking it up. One of these methods is to let the app pretend to be a music player and constantly play some sound clips(just make them silent to not disrupt the users), this way the OS won't terminate your app and will allow it to run in the background.
Doing so of course against Apple's guideline, is highly likely that your app will be rejected from the App Store unless you have very good reasons to convince the reviewers.
My company is right now developing an app that uses similar approach to keep on running, since its an in-house application we don't need Apple to review it.
I m writing ios app on 4.3 simulator and 3.2 xcode
The app every minute with timer NSTimer checks news on some websites and if there are news, the app will make alert message with sound "There are something new on ... website!"
But if i close app or it goes to sleep, timer stops on background. So, i have read many articles like
http://www.codeproject.com/Articles/124159/Hour-21-Building-Background-Aware-Applications
How keep NSTimer when application entering background?
and non of them helped me. my questions:
1) What technologies or tricks should i use to make such app? One good man told me that i need to use only Push Notifications.
2) If to use Push Notifications, do i have to get Developer Program $100/year from apple and stay my Mac book always turned on for creating server for Push Notifications?
thank you!
There are only a few approved uses for running in the background: music, navigation, VOIP. If your app doesn't do one of those things, it can only run in the background for up to 10 minutes, then it will be suspended by the OS.
Push notifications require a server. You either run it yourself, or you use a service like urbanairship.com. A push notification just shows up like an alert on the device. It doesn't make your app wake up, unless the user does it by responding to the alert.
Please read this:
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
If you haven't paid the $100, you can't run your app on a real device. I don't think the simulator properly simulates background states, so debugging background tasks is going to be pretty difficult.