iOS set local notification on a specific day

前端 未结 4 1214
终归单人心
终归单人心 2021-01-21 02:17

I am sure this question is duplicated somewhere, but I can\'t find a solution. I am making an app in which one feature allows the user to select the days and times they will rec

相关标签:
4条回答
  • 2021-01-21 03:09

    I don't think you can schedule notifications with that much flexibility. Just schedule the next one anytime they change it, and when it fires schedule the next one coming up. Only one to worry about canceling and easy to setup.

    0 讨论(0)
  • 2021-01-21 03:11

    Exactly the same way you are doing right now, but create a date differently:

    NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
    [comps setDay:1];
    [comps setMonth:1];
    [comps setYear:2013];
    [comps setHour:10];
    [comps setMinute:10];
    [comps setSecond:10];
    localNotif.fireDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
    
    0 讨论(0)
  • 2021-01-21 03:12

    I know how to set an notification for a certain time in the future, but how do I set a notification for say 6pm on a Monday?

    You can create an NSDate object representing 6pm on the next Monday with the approach showed in How to Get an NSDate for a Specific Day of Week and Time from an Existing NSDate. Then, if you want it to repeat on every Monday you can use localNotification.repeatInterval = NSWeekCalendarUnit. However I'm not sure it's going to work as expected with Daylight saving time.

    0 讨论(0)
  • 2021-01-21 03:20

    I have also searched about it. Below code work good for me. Pass the week day value 1 to 7 Sunday to Saturday and notification body with action which you want to fire and specify your date then notification will come on that specific day.Hope this help you.

    -(void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
        [componentsForFireDate setWeekday: weekday] ; //for fixing Sunday
      //   [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
     //    [componentsForFireDate setMinute:0] ;
     //    [componentsForFireDate setSecond:0] ;
        notification.repeatInterval = NSWeekCalendarUnit;
        notification.fireDate=[calendar dateFromComponents:componentsForFireDate];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    
    }
    
    0 讨论(0)
提交回复
热议问题