iPhone alarm using repeated local notifications - iphone

I'm trying to add an in-background alarm clock feature to an app I'm developing.
I have read up on the UILocalNotification object and its use, and am aware that it is limited to 30 seconds of audio.
I was thinking of scheduling multiple notifications (say, 5 of them) spaced 30 seconds apart to mimic continuous play, but the problem with this approach is that if the user hits Close, I won't be able to cancel any of the subsequent notifications.
As far as I know, it is impossible to remove or hide the Close button without hiding the notification entirely (i.e., setting the alertBody property to Nil).
So, I thought I might use the repeatInterval property to cause the notification to pop up every 30 seconds, but it seems that I can only set the interval to one minute or one second, and nothing in between.
The feature is meant to allow the user to choose between music and beeps for alarm audio; it seems I may have found a way to do the beeps - setting the repeatInterval to one second, create a second's worth of beeps (which would need to be timed to cleanly repeat) and use that as the notification sound.
However, for the music, this approach limits me to playing 30 seconds of audio, followed by a 30-second gap, followed by 30 seconds of audio, and so on.
I know that there is no straightforward solution here, from my reading of other posts and resources; no third-party app has access to the same functionality as the built-in alarm clock. I am hoping that someone has found a workaround or thinks of something really clever.
UPDATE: I have found that the repeatInterval doesn't help me in this case, as I can't cancel the repetitions without launching the app.
For the time being I have decided not to use a notification as an alarm per se, but have changed the feature to be a reminder (more along the lines of what the notification is intended for).
If I come up with a way to implement user-friendly, reliable alarm functionality to an app, I will update this post.

I am afraid you cannot accomplish this..reason being as you stated the 'Close' button. You won't be getting any call back in the app if Close button is tapped. Further even if you present notifications every 30 seconds, then there will be multiple notifications on the screen which user has to view or close. So the user experience will be crappy. I would recommended making it clear to the users that they can not set alarm with a custom sound more than 30 seconds.

Related

Is it possible to run a scheduled timer in a suspended app?

I've read several posts on creating a timer that runs in the background. Is it possible to create a scheduled timer that will continue to run once the app has been suspended/terminated?
Apple's own timer achieves this, but it's not clear how. I am guessing they send a local notification for the timer expiry, and maybe they undertake a date object comparison if the app is brought to the foreground while still in the countdown period. Is this a correct assumption? Any pointers would be helpful.
In short, what you're looking to do is not possible - since the amount of time your app is allowed to run in the background is limited to ~3 minutes. Look at this answer for more details.
You could achieve your goal through the use of a push notification, but that would require a server and won't guarantee it is received at the perfect time.
You might also want to look at this question
It also appears based on this answer that you cannot set a timer in the standard clock app either.
TLDR - You can't create a timer app.

Alarm clock app in iOS

I have to create an alarm clock app for the iPhone in which user can set alarm time and sound.
In doing so, I have used UILocalNotification to set off the alarm.
Now, in UILocalNotification, first we get Notification Alert with option "Close" and "View". If the user taps on "View", then my delegate receives the application:didReceiveLocalNotification: message and the alarm sound plays.
But in the system-native alarm app, we don't see a notification alert; it just directly plays the alarm sound. How can I have my app use this behavior?
I agree with David. iOS built-in alarm app gets a special treatment from Apple to sound > 30 seconds while in the background. It seems to be using some private API to make it sound for a longer time even when it is in the background or closed. This is not available to other apps built outside of Apple, so don't even try to compete with it. If you buy all Alarm clock apps in the App Store (aka: approved by Apple) and try them one by one, you will see that they lack behind the built-in alarm app in the most important function of an alarm: i.e. "Sounding for a long period, in any state the app is in (active, background, or closed)"
Your best bet to simulate a background alarm is to make use of UILocalNotification for 30 seconds. If you really want to wake up your heavy sleeper, schedule to fire up five of these notifications sixty seconds apart or something like that, in the hopes they will wake up before the 5th notification ends.
Notice that with this method, your user who misses the alarm will get 5 popups on their screen when they wake up (less if they wake up during one of the alarm popups), so they'll be spending their first seconds of their morning closing those popups - not the greatest user experience.
Conclusion: Alarm Clock apps are good for foreground use only. Best Alarm app out there that will work in any state is the built-in Clock app made by Apple.
This alarm behavior is not available to iOS apps. You have the ability to create local notifications and add sounds to them, but the sound length is limited. You cannot have a sound continuously play such as on the built-in alarm.
One option is to repeat the alerts. So your app would send an alert, wait some time then send another. You repeat a specified number of times or until the user interacts with your app. I don't think this would provide a great user experience, so I don't recommend it.
I am pretty sure you can have your own custom local notification sound.
Example code localNotif.soundName = #"alarmsound.caf";
Note local notification sound will have to be of only 30 seconds or less to be played. You can get more info here.
If you enable the "Audio, AirPlay, and Picture in Picture" and "Remote notifications" Background Modes, you can use a silent push notification to wake up the device and then start playing audio, and as long as you’re playing audio you won’t be limited to 30 seconds to respond to the notification like usual. This won’t work if the app has been killed manually by the user, but if the user does that you could send a local notification asking them to re-open the app.

How to show custom alert at specific time?

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).

iphone : precisely timed alarms or alerts

What are the options(if any) for precisely timed alarms or alerts from within an iphone app?
My app may need to alert the user in 30 seconds or 5 hours and 23 seconds.
I assume for shorter times I could just use NSTimer, but what is the cutoff point(if there is one) for a timer to be fired within the app?
For longer timed alarms or alerts I've noticed push notifications have been mentioned, can they be timed to the second, are they reliable? Are there other options?
You want Local Notificaitons. They are easy to use, and require no server. You simply set them up, and they fire later wether your app is open or not.
Note that this will require iOS4
Also I believe there is a 60 second window your alert may appear in and you probably dont have percise control down to the second about when they appear. Though I haven't tested this myself.
Take a look at local notifications. You may schedule these using NSDate, which, for example, allows you to schedule an alert in an interval with a precision down to seconds from the current time.
See:
Local and Push Notifications

NSNotification when device time changes (minutes)?

Is there a way I can easily set up a notification when the minutes change on the system time for iOS devices?
I need to do some UI updates for time changes. I'd like the UI to update exactly on minute changes, not just every 60 seconds through an NSTimer trigger.
There are no notifications provided by the API but you could roll your own by having a background thread polling the system time and then sending a custom notification to the UI on the main thread.
However, I wouldn't bother. You can't manage time "exactly" on any device with a software UI. The best you can do is get the UI updates to occur below the threshold of user perceptions. Humans can't really perceive time intervals of less than 100-200 milliseconds (1/10th second). Any UI more precise is wasted. A NSTimer can very reliably hit a 100 millisecond window as long as some other part of the app doesn't hang or bog.
Unless you've tested the interface using a NSTimer and found it wanting, I wouldn't go looking for adding complexity.