2013-02-08 38 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文件中聲明。