Ranga
Ranga

Reputation: 821

How to prevent cancel all the local notifications. Cancel Single notification nor works

I have a table view as follow i did set reminders for each cell using corresponding switch on

-(IBAction)switchingbtn:(id)sender
{
    UISwitch *onoff = (UISwitch *) sender;
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if(onoff.on)
    {
        NSLog(@"Shedule notification");


    int tagValue=[sender tag];

    NSMutableDictionary *dict = (NSMutableDictionary *)[alarmsArray objectAtIndex:tagValue];


    NSDate *firedate = [dict objectForKey:@"date"];
    NSLog(@"fire date is %@", firedate);
    localNotif.fireDate = firedate;

    localNotif.alertBody = @"Start Exercise";
    localNotif.applicationIconBadgeNumber = 0;

    // localNotif.timeZone =[NSTimeZone timeZoneForSecondsFromGMT:0];
    localNotif.timeZone = [NSTimeZone systemTimeZone];
    localNotif.repeatInterval = kCFCalendarUnitDay;


   //  [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; //**Not working**
    [localNotif release];

}

No i need to cancel a preticular 1 noftication for ex 3rd swich cancels 3rd notification

else
    {
// Cancel a notification not works
      //  [[UIApplication sharedApplication] cancelLocalNotification:localNotif];

        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    NSLog(@"cancel notification");
}

Upvotes: 0

Views: 480

Answers (2)

Abhinav Singh
Abhinav Singh

Reputation: 8090

here is the code which you wanted

- (void)CancelExistingNotification {
//cancel alarm
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"notificationID"]];
    if ([uid isEqualToString:[NSString stringWithFormat:@"%i",self.notificationID]])
    {
        //Cancelling local notification

        [app cancelLocalNotification:oneEvent];
        break;
    }
}

}

"self.notificationID" comes from a property on a custom object like alarmObject which are loaded with the help NSUserDefaults application wide.

Upvotes: 0

Andrea
Andrea

Reputation: 26385

The best way to cancel single notification so far, is to create an notication that has a userInfo dictionary, in this dictionary you could add a notification ID value for an id key. You keep track of the notifications ID (storing in a plist, sql database, etc) and when you need to delete a notification you just need to ask the UIApplication instance for the scheduled notif and filter by the ID, when you find the match you just need to send the cancel method for that notification.

Upvotes: 1

Related Questions