Set repeatInterval in local notification

I want to set the recurrence interval to the value that the user selects from a date set. I have a date picker in type countdown mode in my application. If the user selects 4 hours 15 minutes after selecting a date, I set firedate using the following code and signal rings.

[NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]] 

But I want this notification to be repeated every 4 hours 15 minutes until the user cancels it. I did r & d searched a lot, but I can not understand. The code I've used so far:

 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 text that launches the application and is shown in the alert // [localNotification setAlertBody:[alertBodyField text]]; //Set the message in the notification from the textField 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 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 

Please help me decide. Thank you very much in advance.

+4
source share
2 answers

We cannot set a custom value for repeatInterval to UILocalNotification .

The easiest way to achieve this is by creating a new localNotification every 4.25 hours and copying the notification data of the existing one and deleting it when processing the local notification.

Another way is to change firedate while processing local notification.

+1
source

I solved the problem using the following code:

 -(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]]; }} 

Here testDate and refTimeInterval are variables declared in the .pch file.

+1
source

Source: https://habr.com/ru/post/950896/


All Articles