2013-02-08 102 views
4

我想将重复间隔设置为用户从日期选择器中选择的值。我在我的应用程序中使用了类型倒计时模式的日期选择器。如果用户从日期选择器中选择4小时15分钟,使用下面的代码和闹铃开火。在本地通知中设置repeatInterval

[NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]] 

但我希望通知应在每4小时15分钟后重复,直到用户取消为止。我已经做了[R & d搜索了很多,但我不out.The我用至今代码:

localNotification = [[UILocalNotification alloc] init]; 
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]]]; 

if(localNotification.fireDate){ 

    [self _showAlert:@"Time is scheduled" withTitle:@"Daily Achiever"]; 
} 
localNotification.timeZone = [NSTimeZone systemTimeZone]; 


[email protected]"alaram"; 
localNotification.soundName = UILocalNotificationDefaultSoundName; 
[localNotification setAlertAction:@"View"]; 
[localNotification setRepeatInterval:[pickerTimer countDownDuration]]; 

//The button's text that launches the application and is shown in the alert 
// [localNotification setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField's text 
//[localNotification setHasAction: YES]; //Set that pushing the button will launch the application 
[localNotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]]; //Set the Application Icon Badge Number of the application's icon to the current Application Icon Badge Number plus 1 

    localNotification.applicationIconBadgeNumber = 1; 

localNotification.repeatInterval=NSHourCalendarUnit; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //Schedule the notification with the system 

//[alertNotification setHidden:NO]; //Set the alertNotification to be shown showing the user that the application has registered the local notification 

请帮我解决。提前致谢。

+0

嗨,恭喜你已经解决了你的问题。如果你不介意,你可以分享你的解决方案,因为我面临着同样的情况,我想在每天早上和晚上的一天重复两次本地通知,所以无法弄清楚如何做,请如果你能帮助我一些代码 –

回答

1

我们无法为UILocalNotification中的repeatInterval设置自定义值。

您可以通过每4.25小时创建一个新的localNotification来实现此目的,并且复制现有通知的详细信息并在处理本地通知时将其删除。

其他方法是在处理本地通知时更改firedate

+0

我已经通过在接收本地通知被调用时设置新的启动日期来解决它。我已经使用了以下代码: - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {0} testDate = notif.fireDate; NSLog(@“DATE IS%@”,testDate); //处理应用程序运行时的通知 NSLog(@“Recieved Notification%@”,notif); [self playSoundWithNotification:notif]; [self _showAlert:@“Alaram”withTitle:@“Daily Achiever”]; } – rutvi

+0

亚改变firedate也工作! :) –

+1

如果应用程序在后台而不是如何再次安排通知? –

1

我已经解决了使用下面的代码的问题:

-(void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif { 

testDate=notif.fireDate; 
NSLog(@"Recieved Notification %@",notif); 
[self playSoundWithNotification:notif]; 
[self _showAlert:@"Alaram" withTitle:@"Daily Achiever"]; 
} 


-(void)_showAlert:(NSString*)pushmessage withTitle:(NSString*)title 
{ 

UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

[alertView show]; 

if (alertView) { 
    FocusViewController *fvc=[[FocusViewController alloc]initWithNibName:@"FocusViewController" bundle:nil]; 

    [fvc insert:[NSDate dateWithTimeInterval:refTimeIntrval sinceDate:testDate]]; 

}} 

这里testDate和refTimeInterval的变量.PCH文件中声明。