IOS覆盖本地通知
我创建了一个IOS覆盖本地通知,ios,objective-c,xcode,uilocalnotification,Ios,Objective C,Xcode,Uilocalnotification,我创建了一个本地通知,当单击某个按钮(SetButton)时会触发60秒。我现在的问题是,如果再次按下SetButton,它不会覆盖第一次按下,它会显示两个通知,以此类推。我如何确保第二次按下按钮会覆盖第一次按下按钮,并且不会累积通知 - (IBAction)SetButtonPressed:(id)sender { UILocalNotification *localNotification = [[UILocalNotification alloc] init]; l
本地通知
,当单击某个按钮(SetButton
)时会触发60秒。我现在的问题是,如果再次按下SetButton
,它不会覆盖第一次按下,它会显示两个通知,以此类推。我如何确保第二次按下按钮会覆盖第一次按下按钮,并且不会累积通知
- (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;
}
}
看起来你的应用程序中只有一种类型的本地通知 那样的话,你可以保持简单。每当你想安排一个新的-取消前一个
- (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;
}
如果您仅对该特定操作使用通知,则可以使用一次取消所有通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
如果你不想让以前的本地通知触发,你必须取消它。或者,如果您不想保留对以前设置的通知的引用,只需取消所有本地通知。请注意,[UILocalNotification]已被弃用…@RokJarc Hi,感谢您的回答,但我如何取消以前的通知。我仍在学习这种情况下最简单的方法是调用
[[UIApplication sharedapplication]cancelAllLocalNotifications]代码>在注册新代码之前。尝试将其作为设置按钮pressed:
方法的第一行。这样,您就不必保留对以前计划的通知的引用。