Canceling local notification does not work

I spent half my day reading the How to Cancel Local Notification Q & A. In the end, I came up with my solution, but apparently it does not work. I have a table with all my scheduled notifications.

in file H I have

@property (strong, nonatomic) UILocalNotification *theNotification; 

and then in the M file:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; theNotification = [notificationArray objectAtIndex:indexPath.row]; NSLog(@"Notification to cancel: %@", [theNotification description]); // NSLOG Perfectly describes the notification to be cancelled. But then It will give me "unrecognized selector" UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Local Reminder" message:@"Cancel local reminder ?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; [alertView show]; [alertView release]; [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { NSLog(@"Cancel"); }else{ NSLog(@"Ok"); [[UIApplication sharedApplication] cancelLocalNotification:theNotification]; } } 

If I click "Ok", I get: 2012-02-04 03: 34: 48.806 Third test [8921: 207] - [__ NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x890ae90 Program signal "SIGABRT".

If I can fully identify the notification that needs to be canceled, why does he give it to me?

+7
objective-c xcode4 uitableview ios5 uilocalnotification
Feb 04 2018-12-12T00:
source share
2 answers

In my application, I did it like this:

 - (IBAction)cancelLocalNotification:(id)sender { for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) { if ([[lNotification.userInfo valueForKey:@"FlightUniqueIDKey"] isEqualToString:flightNo]) { [[UIApplication sharedApplication] cancelLocalNotification:lNotification]; } } } 

And when I was planning a local notification, I added a key. FlightNo is a unique identifier for notification.

 NSDictionary *infoDict = [NSDictionary dictionaryWithObject:flightNo forKey:@"FlightUniqueIDKey"]; localNotif.userInfo = infoDict; 

Note from Nick Farina: this only works for scheduled notifications; You cannot cancel the notification submitted via presentLocalNotificationNow :

+12
04 Feb 2018-12-12T00:
source share

I found a way so that I can look a little better. If you want to remove localNotification directly from the table, you can add a cancel or delete button to each cell. So:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row]; [cell.textLabel setText:notif.alertBody]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MM/ d/ YYYY"]; NSString *dateOnRecord = [dateFormat stringFromDate:notif.fireDate]; [cell.detailTextLabel setText:dateOnRecord]; UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; cancelButton.frame = CGRectMake(200, 5, 80, 34); [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal]; cancelButton.titleLabel.textColor = [UIColor redColor]; cancelButton.backgroundColor = [UIColor colorWithRed:0.5 green:0.0 blue:0.0 alpha:1.0]; [cancelButton setTag:indexPath.row]; [cancelButton addTarget:self action:@selector(cancelNotification:) forControlEvents:UIControlEventTouchUpInside]; [cell.contentView addSubview:cancelButton]; [dateFormat release]; return cell; } 

And then you enter your button code:

 -(void)cancelNotification:(id)sender { NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; UILocalNotification *notif = [notificationArray objectAtIndex:[sender tag]]; [[UIApplication sharedApplication] cancelLocalNotification:notif]; [self.tableView reloadData]; } 

This is another way to do this. It seems to me a little better to visualize.

+3
Feb 04 '12 at 20:30
source share



All Articles