Search code examples
iphonedateuilocalnotification

Update fire date for local notification and cancel previous notification


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?


Solution

  • 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