简体   繁体   中英

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.

  • 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];
        }

You can add the local notifications upto 64 at single time.

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    NSDate *newDate = [fireDate dateByAddingTimeInterval:(6*60*60)];
    // 6*60*60=6 hour*60 minutes*60 seconds
    // [fireDate dateByAddingTimeInterval:(6*60*60*i)];
    localNotification.fireDate = newDate;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.repeatInterval = 0;
    localNotification.alertBody = alertText;
    localNotification.alertAction = alertAction;
    if(soundfileName == nil)
    {
        localNotification.soundName = UILocalNotificationDefaultSoundName;
    }
    else
    {
        localNotification.soundName = soundfileName;
    }
    localNotification.alertLaunchImage = launchImage;
    localNotification.applicationIconBadgeNumber = 1;
    localNotification.userInfo = userInfo;
    //        NSLog(@"%@", [localNotification description]);
    // Schedule it with the app
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

If you want to keep all the objects of NSNotification , then you need to store that somewhere. ie in Array.

You can also use following methods for NSNotifications operation.

- (void)cancelLocalNotification:(UILocalNotification *)notification - (void)cancelAllLocalNotifications

Make sure you are receiving notifications on

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

As a good practice prior to use the object, make sure you have initialized and set all the values to particular object.

please try this for local notification

-(void)localnotification{

    NSDate *pickerDate = [self.datepicker date];

    // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = pickerDate;
    localNotification.alertBody = self.labelname.text;
    localNotification.alertAction = @"Show me the item";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM