Set repeatInterval in local notification
Asked Answered
B

2

4

I want to set repeat interval to the value which user selects from date picker.I have date picker of type countdown mode in my application.If user selects 4 hours 15 minutes from date picker then I am setting firedate using the following code and alarm rings.

 [NSDate dateWithTimeIntervalSinceNow:[pickerTimer countDownDuration]] 

But I want that notification should repeat after every 4 hours 15 minutes until user cancels it. I have done r&d searched a lot but I can not figure out.The code I have used so far is:

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

Please help me to solve. Thanks a lot in advance.

Belamy answered 8/2, 2013 at 9:31 Comment(1)
hey congrats that you've solved your problem. if you don't mind can you share your solution as i am facing the same situation i want to repeat the local notification twice in a day like morning and evening daily so unable to figure out how to do so please if you can help me with some codeFrancophobe
S
1

We cannot set custom Value for repeatInterval in the UILocalNotification.

The simplest way you can achieve this by creating a new localNotification every 4.25 hours and copy the notification details of the the the existing one and delete it while handling the local notification.

Other way is to change the firedate while handling the local notification.

Signore answered 8/2, 2013 at 10:12 Comment(2)
I have solved it by setting new fire date when receive local notification is called . I have used following code for that: - (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif { testDate=notif.fireDate; NSLog(@"DATE IS%@",testDate); // Handle the notificaton when the app is running NSLog(@"Recieved Notification %@",notif); [self playSoundWithNotification:notif]; [self _showAlert:@"Alaram" withTitle:@"Daily Achiever"]; }Belamy
if application is in background than how to schedule notification again ?Scan
B
1

I have solved the problem using 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 .pch file.

Belamy answered 8/2, 2013 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.