Search code examples
ioslocalnotification

How to implement local notification in iOS?


I am trying to implement local notification call on particular time. Here is my code. Where I'm doing wrong, please help me.
In .m File

NSDateComponents* deltaComps = [[NSDateComponents alloc] init] ;
         [deltaComps setDay:1];
         NSDate* tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:deltaComps toDate:[NSDate date] options:0];


        //
      //  NSDate *date = [NSDate date];
       NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
       // NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)
       //                                                fromDate:tomorrow];

        NSDateComponents *temp = [[NSDateComponents alloc]init];

        [temp setYear:2014];
        [temp setMonth:8];
        [temp setDay:21];
        [temp setHour: 13];
        [temp setMinute:38];

        NSDate *fireTime = [calendar dateFromComponents:temp]; // December 31st 2013 9:00


        // set up the notifier
        UILocalNotification *localNotification = [[UILocalNotification alloc]init];

        localNotification.fireDate = fireTime;
        localNotification.timeZone = [NSTimeZone systemTimeZone];
        localNotification.alertBody=@"Nishant";
        localNotification.alertAction = NSLocalizedString(@"Show", nil);
        localNotification.soundName = @"Gentle Rise.m4a";
        localNotification.hasAction = YES;

and receive notification file in app delegate file

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
   // NSLog(@"%@", notification);
}

Please let me know where I'm doing wrong. Any help will be appreciated.


Solution

  • where you schedule the notification??

    e.g -

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    

    localNotification won't come untill you schedule it.