IOS覆盖本地通知

我创建了一个Local Notification ,当单击某个按钮( SetButton )时触发60秒。 我现在的问题是如果再次按下SetButton ,它不会覆盖第一次按下,它会显示2个通知,依此类推。 如何确保第二次按下按钮会覆盖第一次按下并且没有建立通知?

 - (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; } 

我的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; } } 

如果您仅对该特定操作使用通知,则可以一次性取消所有通知

 [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

您的应用中看起来只有一种类型的本地通知。

在这种情况下,你可以保持简单。 每当你想安排一个新的 – 取消前一个。

 - (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; }