如何将本地通知重复间隔设置为自定义时间间隔?

我正在制作一个iPhone应用程序,它需要本地通知。

在本地通知中有repeatInterval属性,我们可以将单位重复间隔分为mintute,hour,day,week,year等。

我想要重复的时间间隔应该是4个小时。

所以每4小时就会有本地通知。

我不希望用户为每个设置单独的通知。

我希望用户能够将repeatInterval设置为4小时。

我怎么做?

得到了答案

它是一样直的。

您无法创build自定义重复间隔。

您必须使用NSCalendarUnit的内置单位时间间隔

我尝试了所有上述解决scheme,甚至试过其他的东西,但都没有工作。

我已经投入了足够的时间来发现,我们没有办法让它在自定义的时间间隔内工作。

希望这有助于所有寻求解决scheme的人。

感谢所有试图回答我的问题的人。 多谢你们!!

重复间隔只是一个位图常量的枚举 ,所以没有办法自定义repeatIntervals ,只是每分钟,每秒,每周等。

这就是说,没有理由你的用户应该设置每一个。 如果你让他们在用户界面设置了两个值,比如“ Frequency unit: Yearly/Monthly/Weekly/Hourly ”和“ Every ____ years/months/weeks/hours ”,那么你可以自动生成适当的通知,消防date不重复。

 UILocalNotification *locNot = [[UILocalNotification alloc] init]; NSDate *now = [NSDate date]; NSInterval interval; switch( freqFlag ) { // Where freqFlag is NSHourCalendarUnit for example case NSHourCalendarUnit: interval = 60 * 60; // One hour in seconds break; case NSDayCalendarUnit: interval = 24 * 60 * 60; // One day in seconds break; } if( every == 1 ) { locNot.fireDate = [NSDate dateWithTimeInterval: interval fromDate: now]; locNot.repeatInterval = freqFlag; [[UIApplication sharedApplication] scheduleLocalNotification: locNot]; } else { for( int i = 1; i <= repeatCountDesired; ++i ) { locNot.fireDate = [NSDate dateWithTimeInterval: interval*i fromDate: now]; [[UIApplication sharedApplication] scheduleLocalNotification: locNot]; } } [locNot release]; 

我有一个想法如何做到这一点,我已经在我的项目中实现了这一点

首先,用火灾date(例如,每分钟)创build本地通知。 下一步 – 用这个通知的唯一标识填充用户信息(如果你想在将来删除它)和你的自定义期间,像这样:

 -(void) createLocalRepeatedNotificationWithId: (NSString*) Id { UILocalNotification *localNotification = [[UILocalNotification alloc] init]; NSTimeInterval your_custom_fire_interval = 60; // interval in seconds NSDate *remindDate = [[NSDate date] dateByAddingTimeInterval:your_custom_fire_interval]; localNotification.fireDate = remindDate; localNotification.userInfo = @{@"uid":Id, @"period": [NSNumber numberWithInteger:your_custom_fire_interval]}; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } 

之后,在AppDelegate中实现-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

  1. 从用户信息中获取您的自定义时间段。
  2. 改变下个时期的火灾date
  3. 只要把它添加到幻灯片通知!

     NSInteger period = [[notification.userInfo objectForKey:@"period"] integerValue]; //1 NSTimeInterval t= 10 * period; notification.fireDate =[[NSDate date] dateByAddingTimeInterval:t]; //2 [[UIApplication sharedApplication] scheduleLocalNotification:notification]; //3 

如果你想删除这个通知,那么

 UIApplication *app = [UIApplication sharedApplication]; NSArray *eventArray = [app scheduledLocalNotifications]; for (int i=0; i<[eventArray count]; i++) { UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; NSDictionary *userInfoCurrent = oneEvent.userInfo; NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"id"]]; if ([uid isEqualToString:notification_id_to_remove]) { //Cancelling local notification [app cancelLocalNotification:oneEvent]; break; } } 

很重要!

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification不会在后台调用。 因此,您必须设置长时间运行的后台任务,您将在其中再次创build通知。

我已经使用这个代码,它工作正常重复LocalNotification我已经使用LavaSlider代码这个代码实现

  UILocalNotification * localNotifEndCycle = [[UILocalNotification alloc] init]; localNotifEndCycle.alertBody = @"Your Expected Date "; NSDate *now = [NSDate date]; for( int i = 1; i <= 10;i++) { localNotifEndCycle.alertBody = @"Your Expected Date "; localNotifEndCycle.soundName=@"best_guitar_tone.mp3"; localNotifEndCycle.fireDate = [NSDate dateWithTimeInterval:180*i sinceDate:now]; [[UIApplication sharedApplication] scheduleLocalNotification: localNotifEndCycle]; } } 
 -(void)schedulenotificationfortimeinterval:(NSString *)id1 { NSLog(@"selected value %d",selectedvalue); NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"MMM dd,yyyy hh:mm a"]; UILocalNotification *localNotification2 = [[UILocalNotification alloc] init]; // localNotification2. fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:0]; if(selectedvalue==0) { NSLog(@"dk 0000"); localNotification2.fireDate = [formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] ;//now localNotification2.applicationIconBadgeNumber = 1; localNotification2.repeatInterval=NSDayCalendarUnit; } if(selectedvalue==1) { NSLog(@"dk 1111"); for( int u = 0; u <= 2 ;u++) { localNotification2.fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:43200.0*u] ;// 12 hr localNotification2.repeatInterval=NSDayCalendarUnit; localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]]; localNotification2.alertAction = @"Notification"; localNotification2.soundName=UILocalNotificationDefaultSoundName; localNotification2.applicationIconBadgeNumber = 2; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]); } // localNotification2.repeatInterval=NSDayCalendarUnit; } if(selectedvalue==2) { NSLog(@"dk 22222"); for( int u = 0; u <= 3 ;u++) { localNotification2.fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:28800.0*u] ;//8 hr localNotification2.repeatInterval=NSDayCalendarUnit; localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]]; localNotification2.alertAction = @"Notification"; localNotification2.soundName=UILocalNotificationDefaultSoundName; localNotification2.applicationIconBadgeNumber = 3; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]); } // localNotification2.repeatInterval=NSDayCalendarUnit; } if(selectedvalue==3) { NSLog(@"dk 3333"); for( int u = 0; u <= 4 ;u++) { localNotification2.fireDate = [[formatter dateFromString:[NSString stringWithFormat:@"%@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]]] dateByAddingTimeInterval:21600.0*u] ;//6 hr localNotification2.repeatInterval=NSDayCalendarUnit; localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]]; localNotification2.alertAction = @"Notification"; localNotification2.soundName=UILocalNotificationDefaultSoundName; localNotification2.applicationIconBadgeNumber = 4; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]); } // localNotification2.repeatInterval=NSDayCalendarUnit; } // localNotification2.repeatInterval=NSDayCalendarUnit; NSLog(@"date is %@ %@",[MedicationDict valueForKey:@"Starting"],[MedicationDict valueForKey:@"Ending"]); localNotification2.timeZone = [NSTimeZone localTimeZone]; localNotification2.alertBody = [NSString stringWithFormat:@"Friendly reminder to take %@ %@ at %@.",[MedicationDict objectForKey:@"DrugName"],[MedicationDict objectForKey:@"Frequency"],[MedicationDict objectForKey:@"Ending"]]; localNotification2.alertAction = @"Notification"; localNotification2.soundName=UILocalNotificationDefaultSoundName; //localNotification2.applicationIconBadgeNumber = 1; // infoDict = [NSDictionary dictionaryWithObject:id1 forKey:@"did"]; // localNotification2.userInfo = infoDict; // [[UIApplication sharedApplication] scheduleLocalNotification:localNotification2]; // NSLog(@"all notifications %@",[[UIApplication sharedApplication]scheduledLocalNotifications]); // [[UIApplication sharedApplication] cancelAllLocalNotifications];//dk } 

您可以设置任何时间间隔,如果您设置单独的通知每次你想要通知触发。 然后,您必须对其进行pipe理,如果您不是目前允许在后台运行的应用程序,则必须让用户运行应用程序来刷新它们。 完成这个,我可以告诉你这是一个主要的皮塔饼。

notif.repeatInterval = NSDayCalendarUnit;