在本地通知中设置repeatInterval
Posted
技术标签:
【中文标题】在本地通知中设置repeatInterval【英文标题】:Set repeatInterval in local notification 【发布时间】:2013-01-24 00:51:04 【问题描述】:我想将重复间隔设置为用户从日期选择器中选择的值。我的应用程序中有倒计时模式的日期选择器。如果用户从日期选择器中选择 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
请帮我解决。提前非常感谢。
【问题讨论】:
嘿恭喜您解决了问题。如果你不介意你能分享你的解决方案,因为我面临同样的情况我想每天早上和晚上重复本地通知两次,所以如果你能帮助我,我不知道该怎么做一些代码 【参考方案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 文件中声明的变量。
【讨论】:
【参考方案2】:我们无法在UILocalNotification
中为repeatInterval
设置自定义值。
实现此目的的最简单方法是每 4.25 小时创建一个新的localNotification
,然后复制现有通知的详细信息并在处理本地通知时将其删除。
其他方法是在处理本地通知时更改firedate
。
【讨论】:
我已经通过在调用接收本地通知时设置新的触发日期来解决它。我为此使用了以下代码: - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif testDate=notif.fireDate; NSLog(@"DATE IS%@",testDate); // 处理应用运行时的通知 NSLog(@"Recieved Notification %@",notif); [self playSoundWithNotification:notif]; [self _showAlert:@"Alaram" withTitle:@"每日成就者"]; 如果应用程序在后台而不是如何再次安排通知?以上是关于在本地通知中设置repeatInterval的主要内容,如果未能解决你的问题,请参考以下文章