-1
我創建了一個Local Notification
被點擊某個按鈕(SetButton
)時觸發60秒。我的問題現在的問題是,如果SetButton
再次按下,它不會覆蓋先按下,它會顯示2個通知等。如何確保該按鈕的第二次按覆蓋第一次記者並沒有通知的積累?IOS重寫本地通知
- (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;
}
}
你不得不取消先前的本地通知取消所有的通知,如果你不想它開火。或者,如果您不想保留對先前設置的通知的引用,則只需取消所有本地通知。而且要知道,[UILocalNotification]已被棄用... –
@RokJarc您好,感謝的答案,但我怎麼取消以前的以前的通知。我仍然在學習目標C –
在這種情況下,最簡單的方法將被調用'[[UIApplication的sharedAplication] cancelAllLocalNotifications]'只是你之前註冊一個新的。試試它作爲你的'SetButtonPressed:'方法的第一行。這樣,您不必持有對先前計劃的通知的參考。 –