Ios UILocalNofication在一周内的特定时间点火,1天除外
在我的应用程序中,我有一个本地通知。 我从DatePicker那里得到了开火时间,我想在一周内的特定时间(我从UIPickerView获取)和除周六以外的所有工作日发出LocalNotification 编辑Ios UILocalNofication在一周内的特定时间点火,1天除外,ios,objective-c,nsdate,uilocalnotification,Ios,Objective C,Nsdate,Uilocalnotification,在我的应用程序中,我有一个本地通知。 我从DatePicker那里得到了开火时间,我想在一周内的特定时间(我从UIPickerView获取)和除周六以外的所有工作日发出LocalNotification 编辑 UIDatePicker *picker = [[UIDatePicker alloc]init]; [picker setTag:kDatePickerTag]; for (int i = 0; i < 7; i++) { NSDate
UIDatePicker *picker = [[UIDatePicker alloc]init];
[picker setTag:kDatePickerTag];
for (int i = 0; i < 7; i++) {
NSDate *today = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *scheduleDate = [**picker.date** dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
if (componentsForEachDay.weekday != 7) { // To skip Saturday
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.fireDate = picker.date;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"test message";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
UIDatePicker*picker=[[UIDatePicker alloc]init];
[picker setTag:kDatePickerTag];
对于(int i=0;i<7;i++){
NSDate*今天=[NSDate日期];
NSCalendar*gregorianCalendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate*scheduleDate=[**picker.date**dateByAddingTimeInterval:(i*24.0f*3600.0f)];
NSDateComponents*componentsForEachDay=[gregorianCalendar components:NSWeekdayCalendarUnitFromDate:scheduleDate];
如果(componentsForEachDay.weekday!=7){//跳过星期六
UILocalNotification*localNotification=[[UILocalNotification alloc]init];
localNotification.repeatInterval=NSWeekCalendarUnit;
localNotification.fireDate=picker.date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody=@“测试消息”;
localNotification.soundName=UILocalNotificationDefaultSoundName;
localNotification.applicationBadgeNumber=1;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
到目前为止,我有这个代码,但现在我必须设置点火时间,我不知道如何在我的picker.date(给出时间)和weedDay(给出日期)之间合并。
我需要两者都是相同的NSDate对象。
有什么想法吗?本地通知只有四个内置间隔 他们两个都是
case 1:
notif.repeatInterval = NSMonthCalendarUnit;// monthly
break;
case 2:
notif.repeatInterval = NSYearCalendarUnit;// yearly
break;
case 3:
notif.repeatInterval = NSDayCalendarUnit;//Daily
break;
case 4:
notif.repeatInterval = NSWeekCalendarUnit;// weekly
您无法使用LocalNotifications定义自定义间隔,您可能需要创建自己的逻辑来执行此操作如果您想跳过周六,您必须为其他6天中的每一天设置一个通知,并让它每周重复一次
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
for (int i = 0; i < 7; i++) {
NSDate *scheduleDate = [firstScheduledDate dateByAddingTimeInterval:(i * 24.0f * 3600.0f)];
NSDateComponents *componentsForEachDay = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:scheduleDate];
if (componentsForEachDay.weekday != 7) { // To skip Saturday
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSWeekCalendarUnit;
localNotification.fireDate = FIRE_DATE;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = YOUR_NOTIFICATION_MESSAGE;
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
NSCalendar*gregorianCalendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
对于(int i=0;i<7;i++){
NSDate*scheduleDate=[firstscheduleddatebyaddingtimeinterval:(i*24.0f*3600.0f)];
NSDateComponents*componentsForEachDay=[gregorianCalendar components:NSWeekdayCalendarUnitFromDate:scheduleDate];
如果(componentsForEachDay.weekday!=7){//跳过星期六
UILocalNotification*localNotification=[[UILocalNotification alloc]init];
localNotification.repeatInterval=NSWeekCalendarUnit;
localNotification.fireDate=火灾日期;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody=您的通知消息;
localNotification.soundName=UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
}
}
您尝试过什么?我们不会为您编写。为每天创建一个本地通知,使用NSDate/Calander确定它是哪一天。如果是星期天,则不要安排那一天。编辑,这是我目前为止不知道的。我不知道如何做,否则我会问:|检查:我已经处理过本地通知,我已经完成了为了知道它们的间隔是不能改变的,如果你需要每天都收到通知,那么最好将它们安排在每7天,并在周六例外,我也不得不使用这样的逻辑。谢谢你的代码。这行代码是什么意思?׳I*24.0f*3600.0׳@OshriALM它基本上设置了下一个firing date比您的第一次解雇日期晚i*24小时“firstScheduledDate”应为NSdate的当前日期?@OshriALM firstScheduledDate是NSdate的一个实例,NSdate是您的第一次通知解雇日期和时间。