iPhone : Daily local notifications

前端 未结 3 466
眼角桃花
眼角桃花 2020-11-29 04:58

I am trying to implement local notification

This is what I have set

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

    // Add one minute to          


        
相关标签:
3条回答
  •  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];
        }
    
    0 讨论(0)
  • 2020-11-29 05:18

    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.

    0 讨论(0)
  • 2020-11-29 05:43

    I guess what you need is NSDayCalendarUnit.

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

    0 讨论(0)
提交回复
热议问题