-1

I created a Local Notification that triggers 60 seconds when a certain button(SetButton) is clicked. My problem right now is if SetButton is pressed again, it does not override the first press, it displays 2 notifications and so on. How do I make sure that the second press of the button overrides the first press and there isn't a build up of notifications ?

- (IBAction)SetButtonPressed:(id)sender {
      UILocalNotification *localNotification = [[UILocalNotification alloc] init];
      localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
      localNotification.alertBody = @"HEY GET UP";
      localNotification.timeZone = [NSTimeZone defaultTimeZone];
      localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

}

My AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
     if ([UIApplication instanceMethodForSelector: @selector(registerUserNotificationSettings:)]) {
         [application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
     }

     if (localNotification) {
        application.applicationIconBadgeNumber = 0;
     }
}
Arasuvel
  • 2,971
  • 1
  • 25
  • 40
  • 1
    You have to cancel previous local notification if you don't want it to fire. Or simply cancel all local notifications if you don't want to hold a reference to previously set notifications. And be aware that [UILocalNotification] is deprecated... – Rok Jarc Jan 20 '17 at 17:20
  • @RokJarc Hi, thanks for the answer but how do I cancel previous previous notification. I am still learning objective c – John Caine Jan 20 '17 at 17:28
  • In that case the easiest way would be calling `[[UIApplication sharedAplication] cancelAllLocalNotifications];` just before you register a new one. Try it as the first line in your `SetButtonPressed:` method. This way you don't really have to hold a reference to previously scheduled notifications. – Rok Jarc Jan 20 '17 at 17:35

2 Answers2

1

If you only use notifications for that specific action you could cancel all notifications at once using

[[UIApplication sharedApplication] cancelAllLocalNotifications];
Jose Quintero
  • 181
  • 1
  • 4
0

It looks like you only have one type of local notifications in your app.

In that case you could keep it simple. Whenever you want to schedule a new one - cancel the previous one.

- (IBAction)SetButtonPressed:(id)sender {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    localNotification.alertBody = @"HEY GET UP";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = 
      [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
}
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124