4

我想将重复间隔设置为用户从日期选择器中选择的值。我的应用程序中有倒计时模式的日期选择器。如果用户从日期选择器中选择 4 小时 15 分钟,那么我将使用以下代码和警报响铃设置 firedate .

 [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

请帮我解决。提前非常感谢。

4

2 回答 2

1

我们不能在 中设置自定义repeatIntervalUILocalNotification

最简单的方法是localNotification 每 4.25 小时创建一个新的并复制现有通知的详细信息并在处理本地通知时将其删除。

firedate另一种方法是在处理本地通知时更改。

于 2013-02-08T10:12:32.030 回答
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 文件中声明的变量。

于 2013-02-08T10:46:05.760 回答