5

I know there are a few questions here and there regarding how to delete a local notification which might be all or a particular notification. I have also gone through the local notification class reference and found some methods like repeat time interval,fire date,alert body,time zone etc...but I am unable to find out some sort of information regarding how to modify the fire date that is already been set. Say if the user sets a notification with date today and time 4:50 PM, but if the user wishes to modify the set date/time, what's happening is the notification is firing on both occasions. Which is a blunder as far as programming ethics are concerned!

Actually what I want is the previous notification must be cancelled i.e. date must be modified to edited one and notification should be set and fired on the new date.

This is how I set the notification,sample code:

- (void)setNotification
{
    //Set notification after confirmation of saved data

    Class cls = NSClassFromString(@"UILocalNotification");
    reminderNotification = [[cls alloc] init];

    if (cls != nil) 
    {        
       NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
       [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
       NSDate *notificationDate = [dateFormat dateFromString:textField2.text];
       reminderNotification.fireDate = notificationDate;
       reminderNotification.timeZone = [NSTimeZone defaultTimeZone];
       NSString *reminderText = [NSString stringWithFormat:@"%@ 's %@ on %@",textField.text,textField1.text,strDate];
       reminderNotification.alertBody = reminderText;
       reminderNotification.alertAction = @"View";
       reminderNotification.soundName = @"lazy_afternoon.mp3";
       reminderNotification.applicationIconBadgeNumber = 1;
       NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder];
       reminderNotification.userInfo = userDict;
       [[UIApplication sharedApplication] scheduleLocalNotification:reminderNotification];
       [reminderNotification release];
    }
}

How to deal with this task?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Eshwar Chaitanya
  • 697
  • 10
  • 21

2 Answers2

18

Use this method to schedule Notification , Where the notificationID has to be unique

 -(void) scheduleNotificationForDate:(NSDate *)date AlertBody:(NSString *)alertBody ActionButtonTitle:(NSString *)actionButtonTitle NotificationID:(NSString *)notificationID{

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = date;
    localNotification.timeZone = [NSTimeZone localTimeZone];
    localNotification.alertBody = alertBody;
    localNotification.alertAction = actionButtonTitle;
    localNotification.soundName = @"yourSound.wav";

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
    localNotification.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Use this method to cancel Specific Notification with that Notification Id

- (void)cancelLocalNotification:(NSString*)notificationID {
    //loop through all scheduled notifications and cancel the one we're looking for
    UILocalNotification *cancelThisNotification = nil;
    BOOL hasNotification = NO;

    for (UILocalNotification *someNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([[someNotification.userInfo objectForKey:notificationID] isEqualToString:notificationID]) {
            cancelThisNotification = someNotification;
            hasNotification = YES;
            break;
        }
    }
    if (hasNotification == YES) {
        NSLog(@"%@ ",cancelThisNotification);
        [[UIApplication sharedApplication] cancelLocalNotification:cancelThisNotification];        
    }
}

Reference : UILocalNotification

Bala
  • 2,895
  • 1
  • 19
  • 60
  • One small doubt,can I declare Notification ID with in the schedule notification instead of placing it in method itself :) – Eshwar Chaitanya Jun 21 '12 at 13:18
  • Because I need to call [self scheduleNotification] in save button action,if I write it so,then how can I access all those variables and place it in method itself :( – Eshwar Chaitanya Jun 21 '12 at 13:19
  • let me consider that you have the fireDate and alertBody content in [self scheduleNotification] method , so you can rewrite the above method as -(void) scheduleNotificationForNotificationID:(NSString *)notificationID; ignoring those parameter that you already have, and you can that method in save button as [self scheduleNotificationForNotificationID:@"yourUniqueId"]; – Bala Jun 21 '12 at 13:26
  • Oh so you mean to say even though I just call [self setNotification] and without calling this method,I mean the void scheduleNotification method,the notification gets set – Eshwar Chaitanya Jun 21 '12 at 13:32
  • yup and you better change this code NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder]; reminderNotification.userInfo = userDict; in your setNotification method as like this NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:self.textField1.text]; localNotification.userInfo = infoDict; – Bala Jun 21 '12 at 13:34
  • Sorry got your point now,let me give a try at it and see if it works or not,thanks – Eshwar Chaitanya Jun 21 '12 at 13:35
  • because that could be simple to access the notification, All the Best – Bala Jun 21 '12 at 13:37
  • Can you please tell what should be written in this method... -(void) scheduleNotificationForNotificationID:(NSString *)notificationID because I have already scheduled notification in setNotification method na – Eshwar Chaitanya Jun 21 '12 at 13:40
  • you just change the bellow code your setNotification method NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:kReminder]; reminderNotification.userInfo = userDict; in your setNotification method as like this NSDictionary *infoDict = [NSDictionary dictionaryWithObject:self.textField1.text forKey:self.textField1.text]; localNotification.userInfo = infoDict; – Bala Jun 21 '12 at 13:46
  • Yeah I understood that point Mr.Bala,but what I am asking is already the notification is scheduled in setNotification method,so what should I do in "-(void) scheduleNotificationForNotificationID:(NSString *)notificationID" method,should I set the same notification,I mean writing the same code there again – Eshwar Chaitanya Jun 21 '12 at 13:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12861/discussion-between-bala-and-eshwar-chaitanya) – Bala Jun 21 '12 at 13:49
  • Bala,please login to skype,I have a problem,please – Eshwar Chaitanya Jul 24 '12 at 05:56
3

Once you set notification,the only way to edit it ,is canceling the old one and recreate another one,so you can do this way,searching your existing one and cancel it.

for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        if([[aNotif.userInfo objectForKey:@"id"] isEqualToString:nId]) {
            [[UIApplication sharedApplication]cancelLocalNotification:aNotif];
        }
    }

And then create new notification.

Dhruv
  • 2,153
  • 3
  • 21
  • 45
  • Sorry I am unable to understand what is the key here,should I create an id for each notification,if so how to set it,please elaborate me,I am new to local notifications,please see my code on how I am setting notification,thanks :) – Eshwar Chaitanya Jun 21 '12 at 13:15
  • Its simple,you just need to have one unique key that you generate and store it,and using it you can,notification and using same key to cancel it.If you have single notification,you can store it in NSUserDefaults else you need database for multiple entries. – Dhruv Jun 21 '12 at 16:16