Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS在特定日期设置本地通知_Ios_Objective C_Uilocalnotification - Fatal编程技术网

iOS在特定日期设置本地通知

iOS在特定日期设置本地通知,ios,objective-c,uilocalnotification,Ios,Objective C,Uilocalnotification,我确信这个问题在某处重复,但我找不到解决办法。我正在制作一个应用程序,其中一个功能允许用户选择接收本地通知的日期和时间 他们可以选择一天中的任何时间,并可以切换一周中的不同日期(周一、周二、周三等)。通知将每周发送一次。因此,我将用户限制为仅创建3个通知-如果选择了所有7天,我将把repeatInterval设置为daily(一个通知)。如果为每3个通知选择6天,则我每天需要一个单独的通知(总计3x6=18个通知)。很可能只会使用一个通知,因此这很好 我知道如何为将来的某个特定时间设置通知,但如

我确信这个问题在某处重复,但我找不到解决办法。我正在制作一个应用程序,其中一个功能允许用户选择接收本地通知的日期和时间

他们可以选择一天中的任何时间,并可以切换一周中的不同日期(周一、周二、周三等)。通知将每周发送一次。因此,我将用户限制为仅创建3个通知-如果选择了所有7天,我将把
repeatInterval
设置为daily(一个通知)。如果为每3个通知选择6天,则我每天需要一个单独的通知(总计3x6=18个通知)。很可能只会使用一个通知,因此这很好

我知道如何为将来的某个特定时间设置通知,但如何为周一下午6点设置通知

下面是我用来测试的代码。它会在未来设置4秒的警报(我是从
applicationidenterdbackground
调用它的)


我认为你不能灵活地安排通知。只要在他们改变它的时候安排下一个,当它触发时,安排下一个出现。只有一个需要担心取消并且易于设置。

与您现在的做法完全相同,但创建日期的方式不同:

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];
我知道如何设置未来某个时间的通知, 但是我如何设置一个通知,比如周一下午6点


您可以使用中显示的方法创建表示下周一下午6点的NSDate对象。然后,如果希望它在每周一重复,可以使用
localNotification.repeatInterval=NSWeekCalendarUnit
。不过,我不确定夏令时会不会像预期的那样起作用。

我也搜索过。下面的代码对我有好处。将星期日值1到7传递到星期日到星期六,并在通知正文中指定要启动的操作和日期,然后通知将在该特定日期发出。希望这对您有所帮助

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

}

这并不是一个真正的问题——他更多的是询问如何在准确的日期创建通知——fireDate来了。嗯,那么这将在2013年1月1日10:10:10发出警报?我想知道在过去把它设定为一个日期会发生什么。。。如果设置为每周重复,它将在每个星期二自动开火(因为这是13年1月1日)?哦,这确实有效。我可以使用
NSWeekdayCalendarUnit
在工作日和NSWeekCalendarUnit上重复,这样setDay就有了一些效果。明亮的如果您在过去设置了一个日期,我认为通知将在您初始化它的同时立即执行。若应用程序正常工作,用户将什么也看不到,您的应用程序将收到通知。@Patrick,rmaddy:伙计们,你们找到解决方案了吗?
-(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];

}