2016-12-25 56 views
0

這是我的本地通知代碼,我每天使用[localNotification setRepeatInterval:NSCalendarUnitDay];獲得通知,現在我想在日期和時間到期後使用結束日期和時間來停止或取消通知。我應該在那裏實施,任何人都請解釋。在iOS中使用本地通知設置提醒目標C

NSString *startdate = @"10-12-2016 07:00 am"; 
NSString *enddate = @"14-12-2016 07:00 am"; 
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = startdate; 
localNotification.alertBody = [NSString stringWithFormat:@"%@ ",self.nameTextField.text]; 
      localNotification.soundName = @"bell_tree.mp3"; 
      localNotification.alertAction = @"Show me the item"; 
      localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
      localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
    [localNotification setRepeatInterval:NSCalendarUnitDay]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

,這是我的應用程序委託didReceiveLocalNotification編碼,

- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification { 
    UIApplicationState state = [application applicationState]; 
    if (state == UIApplicationStateActive) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder" 
                 message:notification.alertBody 
                 delegate:self cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 

    // Request to reload table view data 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self]; 

    // Set icon badge number to zero 
    application.applicationIconBadgeNumber = 0; 

} 

回答

0

您可以通過下面的代碼取消通知:

取消一個本地通知,這行代碼:[[UIApplication sharedApplication] cancelLocalNotification:theNotification]

您可以在向用戶顯示通知之後在didReceiveLocalNotification中執行此操作,或將該通知傳遞給"notification"反對用戶是否取消它的類/方法。

或者你可以實現「取消」按鈕點擊警報,並在該點擊你可以取消該提醒(localNotification)。

+0

如何檢查接收通知日期和時間是否等於結束日期和時間字符串。那麼只有它可能的權利?,請你解釋一下。 – Jaikannan

相關問題