How to edit a particular scheduled local notification in iPhone? [duplicate] - iphone

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a simple way to edit / modify a UILocalNotification
I have an app in which I am firing local notifications at particular time intervals by selecting time from date picker. I have an edit page where I can change the time of the date picker. This is working fine.
But the problem is I don't know how to edit a scheduled notification. I want that the time that i have selected through the edit page and saved should get saved in the notification.

You can cancel the already scheduled Notification and schedule a new one by computing the time left until the cancelled notification was going to be fired.
To cancel a local notification use cancelLocalNotification: of the UIApplication instance you can get using [UIApplication sharedApplication]

I think ... you could modify the scheduled notification (Instance of UILocalNotification) ...
Here is from the Apple documentation ..
Once you have created an instance of UILocalNotification, you schedule
it using one of two methods of the UIApplication class:
scheduleLocalNotification: or presentLocalNotificationNow:. The former
method use the fire date to schedule delivery; the latter method
presents the notification immediately, regardless of the value of
fireDate. You can cancel specific or all local notifications by
calling cancelLocalNotification: or cancelAllLocalNotifications,
respectively.
So you can cancel the notification and then schedule it again using
scheduleLocalNotification
Here is from the Apple documentation ..
Note: Prior to iOS 4.2, this property was a read-only method. A setter
method has been added and the method has been converted to a
read-write property. When you set this property, UILocalNotification
replaces all existing notifications by calling
cancelLocalNotification: and then calling scheduleLocalNotification:
for each new notification.

Related

Snooze Alarm in Alarm Clock Application in 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

Notify app when iPad date time settings changed

I would like to get notified when ipad's date-time settings is changed. Is there any way for that?.
I am using NSDateFormatter to find whether iPad/iphone time mode is 12 or 24 hr format. NSDateFormatter is seems to take lots of time( seen in time profiling). So I would like to check use it only when settings is changed.
You can do it using two ways:
Implement - (void)applicationSignificantTimeChange:(UIApplication *)application in your app delegate.
Add a observer for UIApplicationSignificantTimeChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(timeChanged:) name:UIApplicationSignificantTimeChangeNotification object:nil];
applicationSignificantTimeChange:
Tells the delegate when there is a significant change in the time.
- (void)applicationSignificantTimeChange:(UIApplication *)application
Parameters
application
The delegating application object.
Discussion
Examples of significant time changes include the arrival of midnight,
an update of the time by a carrier, and the change to daylight savings
time. The delegate can implement this method to adjust any object of
the application that displays time or is sensitive to time changes.
Prior to calling this method, the application also posts a
UIApplicationSignificantTimeChangeNotification notification to give
interested objects a chance to respond to the change.
If your application is currently suspended, this message is queued
until your application returns to the foreground, at which point it is
delivered. If multiple time changes occur, only the most recent one is
delivered. Availability
Available in iOS 2.0 and later.
Declared In UIApplication.h
For more check UIApplicationDelegate_Protocol
How about adding an observer for NSCurrentLocaleDidChangeNotification ? Per Apple, "Re-create any cached date and number formatter objects whenever the current locale information changes."
You may also want to listen to this notification:
NSSystemTimeZoneDidChangeNotification

Notiication when user selects entry from notification center

When a user selects an entry from the push notification center, it triggers the
application:didReceiveRemoteNotification:
method on the application delegate. Is there a way to tell when a push notification is triggered while the user is in the app, and when they select an older push from their notification center?
Is there a way to tell when a push notification is triggered while the user is in the app[...]?
For this I always understood that
application:didReceiveRemoteNotification
is called for every notification received after you start up the app, even if it is on foreground. Although I haven't had the need to do this (and can't confirm it at the moment), so I am calling on theory only. But that's what I can understand from the following on Local and Push Notification Guide.
iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.
has for
Is there a way to tell when [...](a user) select an older push from their notification center?
For this you could add a order(or time) variable to your push payload to be able to understand if the user is selecting/activating the app trough a push older that a previous one selected.
Check this page for more on adding extra data to the Push Notification.

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.

How can I dismiss UILocalNotifications that appear when my app is in the background?

I'm working on an iPhone app that needs to remind a user to check in at regular intervals using UILocalNotifications. If they don't check in for a few hours, they may be reminded a couple times, but I just want to show the latest notification.
Now, if the app is open, I get a callback to didReceiveLocalNotification:(UILocalNotification *)notification, and I can keep track of whether there's a notification showing.
If the app is not running and the user clicks the -action- button, I get a callback to
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
so I can dismiss any old notifications. However, if they click 'cancel', they have to click 'cancel' on a bunch of layered notifications, because as far as I can tell I don't get a callback (how could I, if the app isn't launched) and it doesn't seem like there's a flag or something when creating the UILocalNotification to have newer reminders from an app automatically dismiss other ones.
If the app is in the background but running, it's worse - first, I don't get any sort of callback there if the user click's cancel, so I have the same problem - the user has to click cancel a bunch of times. Second, if they click the action, I get a call to ApplicationDidBecomeActive, but there's no distinguishing between that and when the user just switches back and forth; I can dismiss and reschedule them here, but it doesn't seem to work perfectly, sometimes a few pop up before they're dismissed.
Any suggestions? If there were a way for the notifications to expire automatically that would be great too. I've looked online a bit and haven't found much help, but it seems like a big oversight, so hopefully there's some way to handle this gracefully.
Thanks.
You won't be able to get any callback when the user "cancel"s as you pointed out.
Is it possible to just remind the user once in your case? Only schedule one notification at a time and renew it on app launch/resume.
I have not tried the following yet, but I believe this can be a work around for your case. Use CLLocationManager's startMonitoringSignificantLocationChanges
Every time the device's location changes significantly, your app will be launched in the background with options passed to locationManager:didUpdateLocations: and you can probably schedule a UILocalNotification from there!