我想将重复间隔设置为用户从日期选择器中选择的值。我的应用程序中有类型倒计时模式的日期选择器。如果用户从日期选择器中选择4小时15分钟,那么我使用以下代码设置fireate和警报响。
[NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]]
但是我希望通知应该在每4小时15分钟后重复一次,直到用户取消它为止。我已经做了很多搜索,但我无法弄明白。到目前为止我使用的代码是:
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];
localNotification.alertBody=@"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)
我们无法在repeatInterval
中为UILocalNotification
设置自定义值。
您可以通过每4.25小时创建一个新localNotification
并复制现有通知详细信息并在处理本地通知时将其删除来实现此目的的最简单方法。
其他方法是在处理本地通知时更改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文件中声明的变量。