设置按repeatInterval在本地通知(Set repeatInterval in local

2019-07-19 14:48发布

我想重复间隔设置为哪个用户从日期picker.I选择值具有的类型倒计时模式日期选取器在我application.If用户选择4小时从日期选择器15分钟,然后我设置firedate用下面的代码和报警环。

 [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];


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

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

Answer 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文件中声明的变量。



Answer 2:

我们不能设置自定义价值repeatIntervalUILocalNotification

最简单的方法,你可以通过创建一个新的实现这一localNotification每4.25小时并复制了现有的一个通知的详细信息,并删除它,同时处理本地通知。

另一种方式是改变firedate同时处理本地通知。



文章来源: Set repeatInterval in local notification