Snooze Alarm in Alarm Clock Application in iPhone? - iphone

I've been working on Alarm Clock application and using local notifications to pop the alarm.
I also need to snooze the alarm as per the time interval set by the user in user settings.
The user cannot snooze until the application starts i.e below mentioned method is called.
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
As I'm calling the method to snooze alarm by adding the time interval in firedate and rescheduling the local notification.
The problem is the application comes to foreground and according to Apple's HIG I should not terminate the app or send the app to background automatically and hence user needs to press home button to send the app in background.
Recently, I came across this app. It can snooze the alarm the way I want to.
One more issue is I can select sound to be played from My Music Library in this app.
Any idea how this app manages to do so?

In order to do what you are after there are several things to consider. Your approach of using UILocalNotification is correct so that is a good start.
In order implement a 'snooze' feature properly you will need to consider the app running in both the foreground and the background.
If the app is in the foreground (or the user opens the app from the notification banner from outside) application:didReceieveLocalNotification: will be called and you can simply update your UI to show a snooze button and schedule a new local notification from the duration loaded from NSUserDefaults.
If the app is the in background and the user closes the notification then there is no way to subsequently try and 'snooze' the alarm.
As for playing a custom sound from the iTunes library take a look at the soundName property on UILocalNotification

Related

UILocalNotification without alert

Is it possible to to call didReceiveLocalNotification without displaying Alert when in background?
didReceiveLocalNotification will only be called if you app is in the foreground or when the user clicked to view the notification and you app is restored to the foreground from the background.
You might be able to use a UILocalNotification with out a message, by example update your application icon badge. But then the didReceiveLocalNotification will not be called if you apps in not the in foreground.
No there is no way sadly, UILocalNotication has been designed to notify the user with an alert view, thats how apple intended them to work
You could set notification.alertBody to nil, but then you will not be able to use this local notification when the application is not active

How do i find out what state the application is in as the result of a notification?

I have a UILocalNotification set up, and as far as i can see it i have 5 different scenarios:
The app is not running, the user chooses to view the notification, so it launches the app.
The app is not running, the user chooses to close the notification, then opens the app at a later date.
The app is running in the background, the user chooses to view the notification, so it brings the app to the foreground.
The app is running in the background, the user chooses to close the notification, then opens the app bringing it to the foreground at a later date.
The app is running in the foreground.
How do i deal with these 5 different scenarios?
Put your code into application:didFinishLaunchingWithOptions:. In
the actions NSDictionary you will find the information about the
notification.
You can again check in application:didFinishLaunchingWithOptions: if the local
notification is still active and take appropriate action.
Put your code into applicationWillEnterForeground:
Again the same spot, just check if there are active local notifications.
Here you can check in application:didReceiveLocalNotification: and either notify the user or not.
Not exactly sure what you're after, but the following might answer your question.
From the documentation:
When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. 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.
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.

Home button press , Which AppDelegate method should i use for scheduling a local notification

I would like to schedule a local notification as soon as the user hits the home button.
Which App delegate method should I use in this case :
applicationWillResignActive
applicationDidEnterBackground
applicationWillTerminate
Well I guess I shouldn't use the third one, but what is the difference between the first two ?
Is there any way to distinguish getting interrupted by a phone call/other notification and actually pressing the home button ?
Thanks in advance.
To schedule local notification you shold use applicationDidEnterBackground instead of using applicationWillResignActive because applicationWillResignActive call every time when app get some specific interruption line phone call, sms. You want to schedule notification when user press home button and in this case applicationDidEnterBackground is the appropriate place to do this.
One thing that should be remember before using applicationDidEnterBackground is that this delegate has approximately five seconds to perform any task, if any task in this delegate will take more time then os will terminate your app. You can also request for additional time for execution by using beginBackgroundTaskWithExpirationHandler and then use a secondary thread to perform a task. For more detail about application delegates follow the links -
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
You should use applicationDidEnterBackground.
applicationWillResignActive gets called anytime your app is interrupted such as a phone call or SMS message. In this case if the user ignores these then your app will keep running in the foreground.
applicationDidEnterBackground only gets called when your app actually goes to the background.
You should do this in applicationDidEnterBackground:
applicationWillTerminate will not be
called when the user hits the home
button. With app switching this is
only sent when the user explicitly
quits the app or possibly in low
memory situations.
applicationWillResignActive is
additionally called when the app is
briefly interrupted, say by an SMS or
phone call alert. (Though if the user
then switches to Messages or Phone
app your app will eventually get a
applicationDidEnterBackground
message).
So it sounds like you're specifically interested in the point when the user taps the home button and the app goes to the background. applicationDidEnterBackground is the place.
You could also always schedule the local notification and only respond to it if the app isn't running when it occurs. Not better necessarily, just an option to consider.

Is there a way to make my background iPhone app to go foreground?

Is there a way to make my background iPhone app to go foreground?
Tnx.
A UILocalNotification will bring the app to the foreground if the device is locked, a notification appears, and the user unlocks the device.
A UILocalNotification with an alertAction will display the alert while the device is unlocked, and if the user taps the View button (or whatever you set it to), your app will be brought to the front.
Not for your app, but the user could do it.
You could schedule a UILocalNotification to inform the user that you are done with your task or whatever.
Nope. But you can do something when you feel must do. For example, use remote Apple Push Notification Service (APNS) to notify your user that you want her to bring your app back to the foreground.
Or, as JustSid put it, use local notification to notify your user that you want her attention.

Detect Application States

I want to detect application states and send it to the server. In the new OS4, with multitasking there are some methods available to help detecting the states:
application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:
applicationWillResignActive:
applicationDidEnterBackground:
applicationWillEnterForeground:
applicationWillTerminate:
I read that now, we have to use applicationDidEnterBackground instead of applicationWillTerminate. My problem is that i need them both.
When the user send the app to the background, it has the state sleep. But when the user close the app ( from the multitask bar ) the state is closed. So i need to detect both, when the user send the app to the background and when the user ( or the system ) close it.
Is there anyway or workaround to make this?
I try subscribing to UIApplicationWillTerminateNotification but it doesn´t work.
Thanks in advice.
The application will quit notification is no longer fired on iOS 4 (as I am led to believe).
When the user hits the home button, the app is sent to the background, and you will get the did enter background notification. But when a user closes the app from the multitask bar, or if the system closes it, the app is sent a SIGKIL message and quits immediately, firing no notifications or delegate methods.