0

I have initialize local notification for every 30 seconds. and i want to stop repeating it once user has pressed button for stop Local Notification.

problem is i couldnt find a way of doing it. it keeps repeating every 30 seconds

This is how i have sheducled localnotification

  // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
    localNotification.alertBody = @"Testing Repeating Local Notification";
    localNotification.applicationIconBadgeNumber = 1;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.repeatCalendar = [NSCalendar currentCalendar];

    localNotification.repeatInterval = kCFCalendarUnitSecond;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

I have tried [[UIApplication sharedApplication] cancelAllLocalNotifications]; . but it dosent work.

Im Batman
  • 1,842
  • 1
  • 31
  • 48
  • 1
    Possible duplicate of [Cancelling single occurence of repeating local notification](http://stackoverflow.com/questions/16333687/cancelling-single-occurence-of-repeating-local-notification) – Vizllx Feb 10 '16 at 09:13

3 Answers3

1

Can you set repeatInterval to 0. According to documentation if it is set to zero notification will be fired once. So when stop button is pressed you can do following

localNotification.repeatInterval = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Vishnu gondlekar
  • 3,896
  • 21
  • 35
0

I solved it.

just removed localNotification.repeatCalendar = [NSCalendar currentCalendar]; from above code.

Im Batman
  • 1,842
  • 1
  • 31
  • 48
0

you can remove notification when you get notification in application active state.

just like

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if (application.applicationState == UIApplicationStateActive)
    {
        NSLog(@"Application is active");

        /*
        ...
        do some process
        ...
        */

        //remove notification
        [application cancelLocalNotification:notification];
    }
    else
    {
        NSLog(@"Application is inactive");
    }
}
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32