It's known that UILocalNotification can be only scheduled up to 64. Would it be possible to create an app that runs in background mode that regularly creates new schedules as older ones expire?
For example, you could store the schedule dates in core data and the app regularly checks to see how many have them have expired, and then reschedule more based on that information.
There's no way to keep your app alive in the background indefinitely, or to have it wake up on a schedule to make the check. If you can live with the fact that all 64 notifications might be shown before the app is run again and has a chance to schedule more, then you can do what you want.
That said, you do know that a UILocalNotification can repeat on its own, right? (See repeatCalendar/repeatInterval properties).
https://developer.apple.com/library/ios/DOCUMENTATION/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40009565-CH1-SW3
Related
I am working on an app that keeps people updated with news around a certain event. That means that I don't want to run background fetches all the time. And one more thing, what I understood from Apple's background execution documentation is that it is not guarantee to have those fetch operations scheduled as the minimum fetch interval set.
So is there any way to schedule those fetches guaranteeing those fetch intervals? I know of silent push notifications but I don't want to have a server side solution.
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.
iOS guidelines apparently don't allow to use background tasks for more than 10 minutes. I am designing a cooking timer app that allows the user to set a specific time and begins a count down.
However it appears impossible to set a background task (e.g. using UILocalNotification or adding an NSTimer to [NSRunLoop mainRunLoop]) that runs for more than 10 minutes.
Is there a work around this? How do developers designs apps that trigger timers that last longer than 10 minutes?
Possible solutions:
A: use server service and run the timer remotely, push notification from server once timer finishes to "warn" user. Cons: expensive to run server, time costly to develop.
B: once the app starts keep it active in the foreground (don't allow the screensaver to trigger). Cons: battery expensive.
Any other ideas?
EDIT: I would like the app to work with iWatch. Hence displaying a glance notification on iWatch once the timer should trigger. As this is guided by the iPhone app I would not be able to do so unless the app is active.
The documentation for UILocalNotification says:
A UILocalNotification object specifies a notification that an app can schedule for presentation at a specific date and time. The operating system is responsible for delivering local notifications at their scheduled times; the app does not have to be running for this to happen.
So, this limit of running apps no more than a few minutes in background does not apply with notifications (as the app doesn't have to be running).
For more information, see the Local and Remote Notification Programming Guide.
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.