0

I have passed today's date and a time before current time. Still it is firing instantly as the code executes.

It is giving correct output in logs.

localNotification <UIConcreteLocalNotification: 0x7a8cc990>{
fire date = Tuesday, 21 June 2016 at 12:30:00 PM GMT, time zone = GMT (GMT) offset 0, repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Tuesday, 21 June 2016 at 12:30:00 PM Eastern Daylight Time, user info = {
    "FIRE_TIME_KEY" = "2016-06-21 12:30:00 +0000";
    NotifySound = Default;
    "Program_Id" = 1114;
    "Program_Name" = "Nutrition For Ch1";
    notification = Start;
}}

Here is my code:

NSDate *date = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"yyyy-MM-dd"];
NSString *strDt = [format stringFromDate:date];

NSString *strDate = [NSString stringWithFormat:@"%@ %@",strDt,strStartSelectedDateTime];
date = [formater dateFromString:strDate];
NSDictionary *dataDict = [NSDictionary dictionaryWithObjectsAndKeys:date,@"FIRE_TIME_KEY",
                                          [[remainingScheArr valueForKey:kUser_Program_Mst_Program_Id] objectAtIndex:h],kUser_Program_Mst_Program_Id,
                                          [NSString stringWithFormat:@"%@ For %@",[[remainingScheArr valueForKey:kProgram_Mst_Program_Name] objectAtIndex:h],
                                           [[NSUserDefaults standardUserDefaults]valueForKey:kSelected_Student]],kProgram_Mst_Program_Name,
                                          @"Start",@"notification",
                                          strNotifySoundPath,@"NotifySound",
                                          nil];
[self performSelector:@selector(scheduleNotificationWithItem:andRepeatInterval:) withObject:dataDict withObject:nil];


- (void)scheduleNotificationWithItem:(NSDictionary*)item  andRepeatInterval:(NSCalendarUnit)CalUnit
{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    localNotification.fireDate =  [item valueForKey:@"FIRE_TIME_KEY"];

    localNotification.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;


    localNotification.alertBody = [NSString stringWithFormat:@"Remember: %@ starts at %@.",value,value];

    localNotification.alertAction = NSLocalizedString(@"View Details", nil);

    localNotification.userInfo = item;

    localNotification.repeatInterval = CalUnit;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

     NSLog(@"localNotification %@",localNotification);
}

It is not giving any error. It is successfully firing but not on the scheduled time, on just execution of code it is firing.

Krutika Sonawala
  • 1,065
  • 1
  • 12
  • 30

1 Answers1

0

You are trying to schedule the Local Notification is past.

NSDate *date = [NSDate date]; // Will give you current date time and Notification will fire immediately.

You need to add some time interval to the date.

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60]; //Will fire after 60 seconds.

Hope so this helps.

Rgds,

Amit

Amit Kalghatgi
  • 357
  • 3
  • 16