Search code examples
iosuitableviewlocalnotification

Set Local Notification from Selecting Table cell


Actually This question has been asked so many Times. But I have Confusion Among those. I have tried this.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([[cell.btn backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:@"unchecked.png"]])
    {
        [cell.btn setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];

    UILocalNotification *reminderNote =[[UILocalNotification alloc]init];
    reminderNote.soundName = @"music.mp3";
    [[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];
    reminderNote.alertBody = @"Wish birthday to :%@",[kAppDelegate.commString objectAtIndex:indexPath.row];

    NSString *date =[kAppDelegate.String objectAtIndex:indexPath.row];

    NSDate *dateP = [ dateformat dateFromString:date];
    components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:dateP];
        [components setHour:4];
        [components setMinute:59];
        [components setSecond:10];

    reminderNote.fireDate = [[NSCalendar currentCalendar] dateFromComponents:components];
    }

Where cell is object of UITableviewCell, But There is Not any Type of Notification. I know there is a little Bug , please help me to find out.


Solution

    • Place the last line as [[UIApplication sharedApplication] scheduleLocalNotification:reminderNote]; after reminderNote.fireDate line.

    • log the reminderNote.fireDate and check the format.

    Hope this helps.

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if([[cell.btn backgroundImageForState:UIControlStateNormal]isEqual:[UIImage imageNamed:@"unchecked.png"]])
            {
                [cell.btn setBackgroundImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    
            UILocalNotification *reminderNote =[[UILocalNotification alloc]init];
            reminderNote.soundName = @"music.mp3";
            reminderNote.alertBody = @"Wish birthday to :%@",[kAppDelegate.commString objectAtIndex:indexPath.row];
    
            NSString *date =[kAppDelegate.String objectAtIndex:indexPath.row];
    
            NSDate *dateP = [ dateformat dateFromString:date];
            components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:dateP];
                [components setHour:4];
                [components setMinute:59];
                [components setSecond:10];
    
            reminderNote.fireDate = [[NSCalendar currentCalendar] dateFromComponents:components];
          [[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];
            }