2017-01-20 174 views
-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; 
    } 
} 
+1

你不得不取消先前的本地通知取消所有的通知,如果你不想它開火。或者,如果您不想保留對先前設置的通知的引用,則只需取消所有本地通知。而且要知道,[UILocalNotification]已被棄用... –

+0

@RokJarc您好,感謝的答案,但我怎麼取消以前的以前的通知。我仍然在學習目標C –

+0

在這種情況下,最簡單的方法將被調用'[[UIApplication的sharedAplication] cancelAllLocalNotifications]'只是你之前註冊一個新的。試試它作爲你的'SetButtonPressed:'方法的第一行。這樣,您不必持有對先前計劃的通知的參考。 –

回答

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

如果你只使用通知,告知具體的行動,你可以一次使用

[[UIApplication sharedApplication] cancelAllLocalNotifications];