0

I am working with Alarm App. I am create Alarm using NSLocal notification. The Alarm is working fine. My problem is I need to repeatedly loop the Alatm without interval.

My code:

 UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    [localNotification setFireDate:date];
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [localNotification setAlertAction:@"Launch"];
    [localNotification setAlertBody:msg];

    [localNotification setHasAction: YES];
    localNotification.soundName = soundFile;

    localNotification.applicationIconBadgeNumber = 1;
    localNotification.repeatCalendar = [NSCalendar currentCalendar];
    localNotification.repeatInterval = kCFCalendarUnitSecond;
 [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Any one help me.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
chandrika
  • 394
  • 4
  • 12

1 Answers1

2

check the below answer. Its simple idea. You can add 1 minute and then send LocalNotification repeatly.

int myInt=60;

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setAlertAction:@"Launch"];
[localNotification setAlertBody:msg];

[localNotification setHasAction: YES];
localNotification.soundName = soundFile;

localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatCalendar = [NSCalendar currentCalendar];
localNotification.repeatInterval = kCFCalendarUnitSecond;

NSDate *datePlusOneMinute = [date dateByAddingTimeInterval:myInt];


UILocalNotification *localNotification1 = [[UILocalNotification alloc] init];
[localNotification1 setFireDate:datePlusOneMinute];
localNotification1.timeZone = [NSTimeZone defaultTimeZone];
[localNotification1 setAlertAction:@"Launch"];
[localNotification1 setAlertBody:msg];
[localNotification1 setHasAction: YES];

localNotification1.soundName = soundFile;
localNotification1.applicationIconBadgeNumber = 1;
localNotification1.repeatCalendar = [NSCalendar currentCalendar];
localNotification1.repeatInterval = kCFCalendarUnitSecond;

NSDate *datePlusOneMinute1 = [datePlusOneMinute dateByAddingTimeInterval:myInt];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
[localNotification2 setFireDate:datePlusOneMinute1];
localNotification2.timeZone = [NSTimeZone defaultTimeZone];
[localNotification2 setAlertAction:@"Launch"];
[localNotification2 setAlertBody:msg];
[localNotification2 setHasAction: YES];

localNotification2.soundName = soundFile;
localNotification2.applicationIconBadgeNumber = 1;
localNotification2.repeatCalendar = [NSCalendar currentCalendar];
localNotification2.repeatInterval = kCFCalendarUnitSecond;


 .....
   ....
  ...


[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
.....
....
...

How many times you need. you can create repeatly.

  • Ok Thanks, But i need to stop when click Notification. What can do? – chandrika Jun 05 '13 at 05:41
  • Try this code. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0) { UIApplicationState state = [[UIApplication sharedApplication] applicationState]; if (state == UIApplicationStateActive) { NSLog(@"ACTIVE"); } else { NSLog(@"NOT ACTIVE"); [[UIApplication sharedApplication] cancelAllLocalNotifications]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; } } –  Jun 05 '13 at 05:44
  • Finally a working solution. This enables to repeat notifications, i.e. simulate ringing like Facebook Messenger has on incoming call. – ph4r05 Dec 16 '15 at 14:36