0
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
    return;
//localNotification.fireDate = [NSDate dateWithTimeInterval:timeInterval sinceDate:now];
localNotification.fireDate = [NSDate date];
localNotification.repeatInterval = 5*NSSecondCalendarUnit;
localNotification.alertBody = @"Your alert message";
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

This code generates notification after first 5 second then it goes back to NSMinuteCalendarUnit, I have been trying to work around with this but no help.

I want to set notifications every 5 seconds and then it should fire it until i forcefully want to stop it.

Can somebody please help??

Yasir Ali
  • 11
  • 2
  • 3
  • Take a look at this post: http://stackoverflow.com/questions/13644357/why-local-notification-is-repeated-after-1-minute-when-the-repeat-interval-is-se – CW0007007 Jan 24 '14 at 09:14

3 Answers3

4

The repeatInterval of a UILocalNotification is of type NSCalendarUnit not an time interval. You can only assign a value from the NSCalendarUnit enum to it.

If you want to fire a local notification every 5 seconds you will need to set multiple notifications.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
1

Here is the code. Use this for every 5 minutes instead of every week.

-(void)setLocalNotificationwithOptions :(NSDictionary *)launchOptions
{
     UILocalNotification *localNoti = [launchOptions  objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNoti) {
    NSLog(@"Notification:%@",localNoti);
}

[[UIApplication sharedApplication] cancelAllLocalNotifications];

NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponent12 = [gregCalendar12 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];


[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:46];

UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:@"ENJOY YOUR WEEKEND!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSMinuteCalendarUnit;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];
}

Hope this helps you. Thank you..

Kiran Gaware
  • 122
  • 6
0

if you want to repeat the timer every 5 seconds, UILocalNotification allows until 30 seconds per one sound. So put a 30 seconds sound in your directory, and then

[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *date = "set your notification date"
for (int i = 0; i < 10; i++){
    NSDate *repeatTime = [date dateByAddingTimeInterval:300 * i];
    notification.fireDate = repeatTime;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
HisaakiH
  • 1
  • 1