3

I have added a local notification programmatically like below:

UILocalNotification *eventLocalNotification=[[UILocalNotification alloc]init];
eventLocalNotification.fireDate=myDate;
eventLocalNotification.timeZone=[NSTimeZone defaultTimeZone];
eventLocalNotification.alertBody=@"My notification";
eventLocalNotification.soundName=UILocalNotificationDefaultSoundName;

Can I change the firingDate, timeZone, alertBody, or any other property?

Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
iOmi
  • 625
  • 10
  • 24
  • 4
    Possible duplicate http://stackoverflow.com/questions/3842252/is-there-a-simple-way-to-edit-modify-a-uilocalnotification – John Nov 29 '12 at 08:06
  • Yes ! but unfortunately I couldn't came across to that question. and also It not helps me a lot. – iOmi Nov 29 '12 at 09:56

2 Answers2

2

After searching a lot over internet, I got a thing that we can't edit a UILocal Notification once added. But sure there is another way that I have found.

  1. Get all the Local notification of your device.
  2. Search the respective local notification
  3. Cancel that notification
  4. Create a New one

Below is the method to add the notification.

-(void)setLocalNotification
{
    UILocalNotification *eventLocalNotification =   [[UILocalNotification alloc]init];

    eventLocalNotification.fireDate     =   //set firing Date of NSDate type
    eventLocalNotification.timeZone     =   [NSTimeZone defaultTimeZone];
    eventLocalNotification.alertBody    =   [NSString stringWithFormat:@"An event has arrived\n Event Name: %@",notificationName.Text];
    eventLocalNotification.soundName    =   UILocalNotificationDefaultSoundName;

    if ([repeat isEqualToString:@"Once"]){

        eventLocalNotification.repeatInterval   =   0;

    }else if ([repeat isEqualToString:@"Daily"]){

        eventLocalNotification.repeatInterval   =   NSDayCalendarUnit;

    }else if ([repeat isEqualToString:@"Weekly"]){

        eventLocalNotification.repeatInterval   =   NSWeekCalendarUnit;

    }else if ([repeat isEqualToString:@"Monthly"]){

        eventLocalNotification.repeatInterval   =   NSMonthCalendarUnit;

    }else if ([repeat isEqualToString:@"Yearly"]){

        eventLocalNotification.repeatInterval   =   NSYearCalendarUnit;
    }

    NSDictionary *info  =   [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@",notificationName.text] forKey:@"name"];
    eventLocalNotification.userInfo =   info;
    NSLog(@"notification userInfo gets name : %@",[info objectForKey:@"name"]);
    [[UIApplication sharedApplication] scheduleLocalNotification:eventLocalNotification];
    NSLog(@"Notification created");
}

Below is the function to cancel the notification

-(void)cancelLocalNotification
{
    UILocalNotification * notificationToCancel=nil;
    for(UILocalNotification * aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications])
    {
        NSLog(@"%@",[aNotif.userInfo objectForKey:@"name"]);
        if([[aNotif.userInfo objectForKey:@"name"] isEqualToString:notificationName ])
        {
            notificationToCancel    =   aNotif;
            [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
            NSLog(@"Notification Cancelled");
            break;
        }
    }

}

Hope you will get benefit from it. Best of Luck

iOmi
  • 625
  • 10
  • 24
1

To set your date and time, you will have to use NSDateComponents and instantiate NSDate twice. One for current date and another will be your desirable date. Than set desirable NSDate's instance to fireDate of UILocalNotification's object.

eventLocalNotification.fireDate = desiredDate;

for timezone, keep it to default the way you did.

eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];

Alert body could be anything, just put your context.

eventLocalNotification.alertBody = @"DESIRED CONTEXT";