I want to wake up my application every 24 hours and update something. Is there a way to do it without using silent push notification, location service and voip. For example the app "My data summary" gives the data usage on daily basis and it does not involve push notification service since i was not asked that permission. So how can i achieve this. Any help would be greatly appreciated.
If you remove the silent push, location services and VoIP, all you have left is Background Audio or Background fetch.
With background audio, you could play a silent sound to keep your application running, but the downside is that it might be killed when another application plays the sound.
With background fetch, you cannot reliably start the application, the OS decides when it will fire up the app to fetch data. How ofter it happens depends on how much is user using your application.
There are no other "legal" ways to achieve this kind of functionality on iOS. There are also a lot of resources available online on this topic.
https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
http://www.raywenderlich.com/29948/backgrounding-for-ios
iOS Background Mode
And many more, you need to research background modes more.
Related
In my app, I used VoIP notification to play a long alarm on app not running state as per suggestion Silent Push, But app got rejected in the following points
Your app is not in compliance with Guideline 4.2.1 using the VoIP API in a manner that it is not intended.
2.5.4 Multitasking apps may only use background services for their intended purposes: VoIP, audio playback, location, task completion, local notifications, etc. If your app uses location background mode, include a reminder that doing so may dramatically decrease battery life.
Is there any way to rectify these issues or would be good if we get any alternative to play a long alarm on push receive in app not running state?
I beleive, UNNotificationContentExtension would help to show our custom notification on push receives but how to play automatic audio on push receive,
Can we handle in push message sound key?
or
Can we do this by default UNNotificationContentExtension methods in
mediaPlay() mediaPlayPauseButtonFrame()
But there is no way to play automatic audio here. please correct me if am wrong.
My requirement will be, I need to play a long audio in any app state and audio should stop on user click of stop button or notification(it usually launch our app).
You can't use background location/ VOIP for other purposes, so if you are using them just to keep your app active in the background, Apple will reject it.
Use them in your app only when it is directly relevant to the features and services provided by the app. Location-based APIs/VOIP shouldn’t be used to provide emergency services or alarm systems, etc. Ensure that you notify and obtain consent before collecting, transmitting, or using location data. If your app uses background services, be sure to explain the purpose in your app. Refer to the Human Interface Guidelines for best practices on doing so.
You can use sound key in your push but audio can only be played for maximum 30 seconds. There's no way like audio should stop when user taps on notification.
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 have 8 medicine local notifications which differ from day to day.
They work fine for one day but I want to fire them everyday. I used BackgroundFetch to reschedule the local notifications every time fetch is executed. But my problem here is that background fetch depends on how often the user uses the app. What if the user doesn't open the app more often Also I didn't want to implement silent notifications because it will not wake up the app if the user does not have internet connection. what approach should I use instead of background fetch?
EDIT:
I also thought about location updates in background because my notification times are taken from location of user and calculated accordingly. But will this consume a lot of battery?
Since I've got the same issue in an
app that probably does the same stuff as yours, I'd like to share my solution.
It comes with one compromise works only from >=iOS8.
By using an interactive notifications you can reschedule your notifications in background, of course the user need to interact with the notification, but I think that if you different actions instead of open the app or cancel the notification is possible to have more interested user.
It's all about creating a configuration with actions.
Here you can find a tutorial.
I wrote application for Android that listens on incoming Calendar events and triggers my flow when application in background.
Now I try to do the same for iOS.
I read a lot of stuff and ran some demos that use EKEventStore and background fetch.
However this approach does not satisfy me.
Is there other way to do that?
[EDIT]
Application uses public API only.
Thanks,
The short answer is you can't. Apple will not allow any app to do things like this in the background, as it requires turning the CPU on when it would otherwise be turned off.
When your app is running, you can read all events in the near future (say, two weeks out?) and setup a "local" push notification for the event time.
Also, if the user launches your app fairly often then Apple will allow your app to refresh using Background App Refresh. The device will typically do this in the morning before whatever time the user usually wakes up and turns their phone on for the first time each day, and throughout the day also, based on the user's activity. You can do anything you want during the background app refresh.
Another spanner in the works is how Apple's full disk encryption works. If the phone is locked, there really isn't much your app can do. Most data on the phone is encrypted with the user's passcode. Until the user enters their passcode not much can be done. In this case background app refresh will run as soon as they enter their passcode, but before they launch your app.
Maybe you could have a remote server send push notifications to the phone as well. Not sure if this is an option for you at all or not. Obviously this means the server would need access to the user's calendar data.
In general, the Android model of background execution doesn't apply to iOS.
You can register for calendar events as shown below, but your application will only be notified when it is running. On iOS, that means it is either in the foreground, recently backgrounded and still running, or has been launched/woken up by the system as part of a designated background mode (e.g. background fetch, location updates, VoIP, etc.).
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveEventStoreChangedNotification:)
name:EKEventStoreChangedNotification
object:eventStore];
If your application has a need for one of the designated background modes, you will at least get notifications from time to time (when you are woken up). I've seen approved apps use location updates just to get more execution time, of course YMMV.