views:

1040

answers:

3

Hello guys!

I have a problem with my UILocalNotification.

I am scheduling the notification with my method.

- (void) sendNewNoteLocalReminder:(NSDate *)date  alrt:(NSString *)title{
// some code ...
UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

 if (localNotif == nil)  
  return;

 localNotif.fireDate = itemDate; 
 localNotif.timeZone = [NSTimeZone defaultTimeZone];
 localNotif.alertAction = NSLocalizedString(@"View Details", nil); 
 localNotif.alertBody = title;
 localNotif.soundName = UILocalNotificationDefaultSoundName; 
 localNotif.applicationIconBadgeNumber = 0;

 NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"id"]; 
 localNotif.userInfo = infoDict; 

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
 [localNotif release];

}

Its work fine and I'm correctly receiving the notification. The problem is when I should cancel the notification. Im using this method.

    - (void) deleteNewNoteLocalReminder:(NSString*) reminderID noteIDe:(NSInteger)noteIDE{

[[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification ????

    }

Im not sure what to do here, how do I know wich UILocalNotification object i should delete? Is there a way to list all notifications? The only thing I have is a ID on wich reminder I should delete. I was thinkin about to save the UILocalNotification object in my "Note" object and get it that way, and when I saving to my SQLite database serialize the object and so on ... is there a smarter way?

+2  A: 

You can get a list of all scheduled notifications from scheduledLocalNotifications or you can cancel them all:

  [[UIApplication sharedApplication] cancelAllLocalNotifications];
progrmr
I know about the cancelAllLocalNotifications but I just want to cancel a specific. ScheduledLocalNotifications maybe do the work .... I will try.
f0rz
A: 

My solution is to archive UILocalNotification object you have scheduled with NSKeyedArchiver and store it somewhere (in a plist is preferred). And then, when you want to to can cancel the notification, look up the plist for the correct data and use NSKeyedUnarchiver to unarchive. The code is pretty easy:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice];

and

UILocalNotification *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data];
hiepnd
A: 

My solution is you when you create UILocalNotification at that time you create one NSMutableDictionary and store that notification as a value for key as your ID and put this NSMutableDictionay to your NSUserDefaults

So when you want to cancel any particular localnotification at that time you write [dictionary valueforkey @"KEY"] where as a key you pass your id so you get that particular local notification and pass it to

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

Mahek