Fire a notification at a specific day and time every week

给你一囗甜甜゛ 提交于 2019-11-27 02:53:17

问题


I want to trigger a UILocalNotification every Sunday at 8PM, however, I'm having a fire date every day at 8PM.

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *now = [NSDate date];
    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit |   NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
    [componentsForFireDate setWeekday: 1] ; //for fixing Sunday
    [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
    [componentsForFireDate setMinute:0] ;
    [componentsForFireDate setSecond:0] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;
    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"New updates!"] ;
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"];
    notification.repeatInterval= NSDayCalendarUnit ;
    notification.soundName=UILocalNotificationDefaultSoundName;

    NSLog(@"notification: %@",notification);//it indicates that the notif will be triggered today at 8PM and not Sunday.

    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

Thank you.


回答1:


You missed the NSWeekCalendarUnit in the NSDateComponents init function. Add the NSWeekCalendarUnit to it and set the repeatInterval to NSWeekCalendarUnit, then output is

next fire date = Sunday, November 24, 2013 at 8:00:00 PM  

The code is here:

  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;

  //...
  notification.repeatInterval = NSWeekCalendarUnit;
  //...



回答2:


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. Time will automatically set when you set date.

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];
}


来源:https://stackoverflow.com/questions/20110805/fire-a-notification-at-a-specific-day-and-time-every-week

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!