How to show custom alert at specific time? - iphone

I'm working on an alarm/reminder application and want to show a custom alarm view when specified time is reached.
From what I understand, there are various (real and hypothetic) ways of doing it:
UILocalNotification. It would be perfect, but alert customization features are insufficient, e.g. I can't show a custom alert view, I can't play a sound for more than 30sec etc.
Keep application in the background and use NSTimer or other means to schedule execution after some time. Most likely won't work as the app may be killed any time.
Start my app at specified time to receive the notification. As far as I know it's not allowed at all.
After spending several days searching for a solution, I would give up and stick with UILocalNotification, but I DO know an app that shows custom alert view at specific time AND is available in AppStore. The app is named "Alarmed" and from what it looks like (last app view shown on alarm time before an actual alert; alarm still triggers even after the device is restarted) they found a way to start their app at specific time and still pass Apple's review.
Any ideas how this can be done?
Thanks.

It's not possible to show notifications other than what UILocalNotification offers you, unless the app is running in the foreground. (I just tried to install "Alarmed" and it uses a regular UILocalNotification like you'd expect).

Related

How to run a ~30sec process in the background every hour (iphone app)

I have an iphone app that has a 30second process that does some network IO. Basically, while the app is in the background, i want this process to run every hour (actually once a day, but if it fails i want it to re-run in an hours time).
With the background features of ios 4, is this possible? If so, how? What are the limitations that i'll come up against?
Thanks so much!
Take a look at Apple's documentation about running code in the background.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
There are few different ways of approaching backgrounded tasks. The only apps that can have fully backgrounded processes are "audio", "voip" and "location" apps, and this needs to be declared in the Info.plist.
If your app is not of this type, you'll probably find it difficult to do what you want easily. There are methods which allow you to keep your app alive in the background for a finite period of time (also at that link), but eventually your app will be shut down.
Local Notifications will only prompt the user to open the app - do you really want to have an alert pop-up on the phone every 30 seconds?
I was making some kind of similar research, have a look at this SO answer in case you didn't manage to find it before. Applications like DataMan or Data Usage must have some sort of periodic code execution in the background, so I'm not 100% convinced that what you're asking for is impossible..
I believe that Using Local notifications will help....
check following....
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
An application can create and schedule a local notification, and the operating system then delivers it at the schedule date and time. If it delivers it when the application is not active in the foreground, it displays an alert, badges the application icon, or plays a sound—whatever is specified in the UILocalNotification object. If the application is running in the foreground, there is no alert, badging, or sound; instead, the application:didReceiveLocalNotification: method is called if the delegate implements it.
The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.

Updating application badge at midnight with options: app not launched or in background, badge number can decrease

I'm reading many things about Local Notifications and the way they can help to update the app badge number.
I'd like to update this badge at midnight, and set its value to a number I can't know before midnight. So I'd like, if possible, to launch a function at midnight that would update/load some datas, check the number to display, and display it on the badge.
Of course, the number to display is not relative to the displayed number, and can be lower or greater.
This is expected to work :
- When app is in background
- When app is in foreground
- When app is not launched
would you know a way to do this, even partially ? If not possible, let say I can make things to know the badge number before midnight... Would this help you for a working answer ? This is not wanted, but if I don't have the choice to make this work, I'll plan to write tons of useless code to do this...
Unless your application fits into one of the "official" multitasking cases (VOIP, location, and background audio), the only way you're going to be able to dynamically set the badge number when your application isn't running is with push notifications... applications can't run code in response to a local notification (until the user presses the "View" button in the notification popup, at least).
See this document for everything you could ever want to know about local and push notifications in iOS.
You can't reliably ensure that your local app will run when it needs to unless you register it as a VoIP app or as requiring continuous location monitoring; either of these, in an app that's not in fact used for VoIP or navigation, will almost certainly result in an App Store rejection.
Your best bet is to send the badge as a push notification. Assuming that a server you control can know what the badge number is supposed to be, it can send a notification with just that badge value—not necessarily an alert popup or sound—to your app. Urban Airship is a good, simple way to send push notifications without having to do a lot of configuration and setup.
You very well may be able to do this. Look at the Apple Guide to running code in the background.

Is there a safe way to schedule an alert for an calendar app?

I want to make a special calendar app, but I am afraid it's not possible to safely schedule an alert for an event.
For example: I set up an alert for an event which starts in 3 months. I want to get notified 2 days before the event starts. In iOS 4 there is multitasking, so my app could run in the background all the time.
But now lets imagine it's a hardcore iPhone user who plays huge memory-intensive games all the time. At some point, iOS might kill my background app. Or the user might restart the device and forget to launch my app. So it could happen that the alert never happens. Bad thing.
Is there a safe way to ensure that an scheduled alert is thrown at the user, just like it is the case with the built in alarm clock app or the calendar app?
I'm going to bring back the EventKit notification - use event kit to schedule a calendar entry with an alert, and embed in there a URL that will open your app.
You could also use local notifications but this way the user will be able to see the upcoming event when reviewing the calendar, and even modify slightly if need be. They can't mess with a local notification once it's in place...
You want to use UILocalNotification for this.
EventKit will make it pop up in the user's calendar, maybe not what you want here.
BTW: Multitasking is really more "fast switching" than backgrounding in iOS... you won't be able to run arbitrary code in background, and you should expect to be killed anytime.

How do I get a codeblock to run on the iPhone once every 24 hours at midnight if my app is suspended (multitasking)

Was reading through the iOS4 documentation for multitasking, and couldn't figure it out.
I basically need to update the badge count on my app's icon after midnight each day as long as the app is running in suspended mode (with multitasking).
I know this has to be possible, just can't figure out the best way to do it.
Thanks.
iOS "multitasking" is very specific. There's an important distinction between states here:
Suspended: Your app resides in memory, but does not receive any execution time. This is really only useful for fast app switching.
Background: In a few particular cases, you can request that the OS to run your app in the background. (Playing audio, location, finishing a long task, voip.)
So, the short answer to your question is, "you can't."
Here are a couple useful links on iOS multitasking, such as it is.
Understand an Application's States and Transitions
Executing Code in the Background
You could use a UILocalNotification to set the badge (without an alert) but you can't increment the badge because you're app does not have the opportunity to execute any code when the notification fires.
You can schedule up to 64 notifications in advance, one at midnight for the next 64 days, each one setting a new badge number. It would make a lot more sense to schedule a repeating notification but since your app can't execute code it can't increment the badge number that doesn't work.

How to implement an alarm with iPhone SDK 4.0

I have an idea for a pretty unusual alarm clock fir the iPhone. But as of now I have some thoughts on how to actually implement this. First off: forgetting about background services for now, how would I do the actual timer that fires the alarm etc? A separate thread? Or does the SDK include any nice alarm features I missed? Of cause I need to be as battery efficient as possible. But for now I do no background process.
Please advise me on this as it is a crucial concept of this app, if it will work or not.
I would create a local notification. Compared to NSTimer, local notifications have the advantage that they work regardless of whether your app is running or not.
You must create a UILocalNotification object and schedule it with scheduleLocalNotification, it is quite simple but there are strong limitations. You app may or may not be notified that the alarm occurred. When the alarm is presented, it will be in form of a AlertView. If the user taps "Close" you do not get a notification. If he taps "View Details", then you get the event didReceiveLocalNotification, your app moves to the foreground and can do whatever you want.
You can also register with iOS4 to receive updates on location change. The parsing of XML can be done but again there are limitations as to how much time you have to run methods in the background.
All information along with sample code can be found here: http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html