Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-01-2010, 03:19 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 49
schimanke is on a distinguished road
Default Canceling a single Local Notification

Here is what I'd like to do...

The user should be able to schedule a local notification via the following action sheet:



Tapping the first button will fire the notification 15 minutes prior to a given date and time. When the user returns to the action sheet and taps the second option, which will fire the notification 30 minutes prior to a given date and time, I'd like to cancel the old notification and and schedule the new one instead. Finally the third button should cancel any notification scheduled from this action sheet.

This is the interesting part of the code I currently use:

Code:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
               
     if (buttonIndex == 0) {
          
     [[UIApplication sharedApplication] cancelAllLocalNotifications];     
                    
     UILocalNotification* localNotification = [[UILocalNotification alloc] init];               
     localNotification.alertBody = alert;
     localNotification.fireDate = myNewDate;
     localNotification.timeZone = [NSTimeZone localTimeZone];
     localNotification.soundName = UILocalNotificationDefaultSoundName;//alarm.caf
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
     [localNotification release];

     }
               
     else if (buttonIndex == 1) {
                    
     [[UIApplication sharedApplication] cancelAllLocalNotifications];
                    
     UILocalNotification* localNotification = [[UILocalNotification alloc] init];               
     localNotification.alertBody = alert;
     localNotification.fireDate = myNewDate;
     localNotification.timeZone = [NSTimeZone localTimeZone];
     localNotification.soundName = UILocalNotificationDefaultSoundName;//alarm.caf
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
     [localNotification release];

     }
               
     else if (buttonIndex == 2) {
                    
     [[UIApplication sharedApplication] cancelAllLocalNotifications];
               
     }
               
}


The above code works almost like I want it. But I use this in six different places within my app, so cancelAllLocalNotifications is not an option. I guess I have to use something like this:

Code:
[[UIApplication sharedApplication] cancelLocalNotification:notification];
Unfortunately I just can't seem to get it to work. The app always fires both notifications.

Does anybody have an idea how to accomplish this? Any help is highly appreciated! Thanks in advance!

Last edited by schimanke; 08-01-2010 at 03:31 PM.
schimanke is offline   Reply With Quote
Old 08-31-2010, 07:42 AM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

I store in the UILocalNotification object an userInfo dictionary that has my appointment id. Then when I need to reschedule the appointment's notifications, I just search within the UIApplication's scheduledNotifications method, the one I need and manually unschedule it.
nobre84 is offline   Reply With Quote
Old 09-10-2010, 02:13 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 12
gammapoint is on a distinguished road
Default

OK Finally I got it.

Following code creates an alarm for me.

First: I also store the alarm object in DB using following:
Code:
NSManagedObject *aVacationMO = nil;
... code for core data -- see apple tutorials.
//save the alarm object created, into DB
		[aVacationMO setValue:[NSKeyedArchiver archivedDataWithRootObject:alarm] forKey:@"alarm"];
In my situation 2 identifiers uniquely identify the alarm( local notification). So I store them in the userInfo.

Code:
// An alarm can be uniquely identified by the time stamp & the event type
-(UILocalNotification * )setAlarm:(NSString *) title start:(NSDate *) startDate reminderIntervalIndex:(NSInteger) remindBefore repeatIntervalIndex:(NSInteger) repeat type:(eventType) etype{
	
	UIApplication *app = [UIApplication sharedApplication];
	NSDate *fireDate = [NSDate dateWithTimeInterval:-1 *[self covertToTimeInterval:remindBefore]  sinceDate:startDate];
	
	UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
	if (alarm)
	{
		alarm.fireDate = fireDate;
		alarm.timeZone = [NSTimeZone defaultTimeZone];
		alarm.repeatInterval = [self covertToCalUnit: repeat]; // default is 0
		alarm.hasAction = YES;
		alarm.soundName =UILocalNotificationDefaultSoundName;
		

		alarm.alertBody = title;
		
	
		alarm.userInfo = [NSDictionary  
					  dictionaryWithObjects:[NSArray arrayWithObjects:fireDate,[NSNumber numberWithInt:etype],nil]  
						  forKeys:[NSArray arrayWithObjects: @"date",@"type",nil]];
		
		[app scheduleLocalNotification:alarm];
		NSLog(@"Added an event with title: %@ start: %@ interval %d", title, fireDate, repeat);
		return alarm;
	}
	
	return nil;
}

For cancel requests, I look for the those 2 identifiers to match the alarm.
Code:
-(void) cancelAlarm:(UILocalNotification *)alarm {

	NSDate *alarmDate = [alarm.userInfo valueForKey:@"date"];
	NSNumber *alarmType = [alarm.userInfo valueForKey:@"type"];
	UIApplication *app = [UIApplication sharedApplication];
	NSArray *alarmArray = [app scheduledLocalNotifications];

	NSLog(@"Trying to cancel alarm %@ date date:%@, type:%@", alarm.alertBody, alarmDate, alarmType);
	for (int i=0; i<[alarmArray count]; i++) {
		UILocalNotification* oneAlarm = [alarmArray objectAtIndex:i];
		NSDictionary *userInfoCurrent = oneAlarm.userInfo;
		NSDate *dateCurrent = [userInfoCurrent valueForKey:@"date"];
		NSNumber *typeCurrent = [userInfoCurrent valueForKey:@"type"];
		NSLog(@"Found scheduled alarm with date:%@, type:%@", dateCurrent,typeCurrent );
		if (dateCurrent == nil || typeCurrent == nil) continue;
		if (oneAlarm == alarm || ([dateCurrent compare:alarmDate] == NSOrderedSame && [typeCurrent compare:alarmType] == NSOrderedSame)) {
			NSLog(@"Cancelling alarm: %@", oneAlarm.alertBody);
			[app cancelLocalNotification:oneAlarm];
		}
	}

}

Last edited by gammapoint; 09-11-2010 at 01:47 AM.
gammapoint is offline   Reply With Quote
Old 09-13-2010, 01:33 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 49
schimanke is on a distinguished road
Default

Thanks for your help! You pointed me in the right direction and I was able to solve it. Since the firedate is unique in each of my cases I now use it to identify the notification and cancel it.

Thanks again!
schimanke is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 321
9 members and 312 guests
alexP, gordo26, headkaze, mistergreen2011, nobstudio, Objective Zero, rayjeong, revg, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:18 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0