9

I am trying to implement local notification

This is what I have set

    // Current date
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time
    NSDate *dateToFire = [date dateByAddingTimeInterval:20];

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

Instead of 20, I want to put a fixed time(daily)to start push.

For ex:I want to push notification pop up at every 6:00AM.

How can do that ?

Thanks

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Shishir.bobby
  • 10,994
  • 21
  • 71
  • 100

3 Answers3

27

You just need to properly create a NSDate object to be your fire date (time). Instead of using [NSDate dateByAddingTimeInterval: 20], use something like this:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: 6];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];

Here are the Apple NSDateComponents API docs

And then when you add the date to the notification, set the repeat interval to one day:

[localNotification setFireDate: dateToFire];
[localNotification setTimeZone: [NSTimeZone defaultTimeZone]];
[localNotification setRepeatInterval: kCFCalendarUnitDay];

As with all date related code, make sure to test how this works during the switch to daylight savings time, if your time zone uses daylight savings time.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • on console i tried out to find the fire date,which is "2012-07-03 00:30:00 +0000" i am not sure, will this fire everyday? – Shishir.bobby Jul 04 '12 at 03:27
  • @iscavengers, It will fire every day if you set the repeat interval to `kCFCalendarUnitDay`, as I showed in the code above – Nate Jul 04 '12 at 07:23
  • how this will be translated to SWIFT? – Yestay Muratov Jun 18 '15 at 07:21
  • @YestayMuratov, this is an old question. I'd recommend asking a new question [with the Ask button](http://stackoverflow.com/questions/ask), specifying that you want answers for Swift. Thanks. – Nate Jun 18 '15 at 07:40
  • What to do if i wants to fire it everyday with 3 Different times ? – Mohit Jan 06 '17 at 11:27
3

I guess what you need is NSDayCalendarUnit.

You can check this answer. And here is another tutorial worth reading.

Community
  • 1
  • 1
Selkie
  • 1,773
  • 1
  • 14
  • 21
1
 NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10];
    UIApplication* app = [UIApplication sharedApplication];

    UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease];
    if (notifyAlarm)
    {
        notifyAlarm.fireDate = alertTime;
        notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
        notifyAlarm.repeatInterval = 0;
        notifyAlarm.soundName = @"Glass.aiff";
        notifyAlarm.alertBody = @"Staff meeting in 30 minutes";

        [app scheduleLocalNotification:notifyAlarm];
    }
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
Girish Chauhan
  • 167
  • 1
  • 4