2014-12-05 19 views
0

我使用本地通知。我想刪除已經安排notifications.I不知道在哪裏寫的code.Here是我的代碼..如何刪除應用程序中已經預定的LocalNotifications?

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; 

    // Get the current date 
    NSDate *pickerDate = self.selectedDate; 
    NSLog(@" self.selectedDate %@", self.selectedDate); 
    // Break the date up into components 
    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
                fromDate:pickerDate]; 
    NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) 
                fromDate:pickerDate]; 

    // Set up the fire time 
    NSDateComponents *dateComps = [[NSDateComponents alloc] init]; 
    [dateComps setDay:[dateComponents day]]; 
    [dateComps setMonth:[dateComponents month]]; 
    [dateComps setYear:[dateComponents year]]; 
    [dateComps setHour:[timeComponents hour]]; 
    // Notification will fire in one minute 
    [dateComps setMinute:[timeComponents minute]]; 
    [dateComps setSecond:[timeComponents second]]; 
    NSDate *itemDate = [calendar dateFromComponents:dateComps]; 

    NSLog(@"itemDate %@",itemDate); 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    if (localNotif == nil) 
     return; 
    localNotif.fireDate = itemDate; 
     NSLog(@"itemDate %@", localNotif.fireDate); 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

    // Notification details 
    localNotif.alertBody = [_titleTextFieldObj text]; 
    // Set the action button 
    localNotif.alertAction = @"View"; 

    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber =[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

    NSLog(@" localNotif.applicationIconBadgeNumber ++ %ld", (long)localNotif.applicationIconBadgeNumber); 



    // Specify custom data for the notification 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[_titleTextFieldObj text] forKey:@"someKey"]; 
    localNotif.userInfo = infoDict; 
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    //UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row]; 
    NSLog(@"notif %@",notificationArray); 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

在這裏,我寫的通知移除....

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ 
    application.applicationIconBadgeNumber=1; 
    UIApplication *app = [UIApplication sharedApplication]; 
    NSArray *eventArray = [app scheduledLocalNotifications]; 
    for (int i=0; i<[eventArray count]; i++) 
    { 
     UILocalNotification* oneEvent = [eventArray objectAtIndex:i]; 
     NSDictionary *userInfoCurrent = oneEvent.userInfo; 
     NSLog(@"userInfoCurrent %@",userInfoCurrent); 
     NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]]; 
     NSLog(@"uid %@",uid); 
     if ([uid isEqualToString:[notification.userInfo objectForKey:@"someKey"]]) 
     { 
      //Cancelling local notification 
      [app cancelLocalNotification:oneEvent]; 
      break; 
     } 
    } 
    if (notification) { 
     NSLog(@"notify %@",notification); 
     NSString *custom=[notification.userInfo objectForKey:@"someKey"]; 
     NSLog(@"custom %@",custom); 
     NSString *newString = [custom stringByReplacingOccurrencesOfString:@" " withString:@""]; 

     NSLog(@"newString %@",newString); 

     NSLog(@"custmky%@",notification.description); 
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:newString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     alert.delegate=self; 
     [alert show]; 


    } 


} 

我是新來UILocalNotifications和Objective-C。任何人都可以請幫我....

回答

0

您可以通過比較火取消UILocationNotification日期已定UILocalNotifications或使用其身。 一個火日期取消的例子:

UIApplication *application = [UIApplication sharedApplication]; 
NSDate *dateToCancel = nil; // Set this to the date you want to cancel 
for (UILocalNotification *notification in [application scheduledLocalNotifications]) 
{ 
    if (notification.fireDate == dateToCancel) 
    { 
     [application cancelLocalNotification:notification]; 
    } 
} 

現在,如果你有一個通知指針你可以撥打當地的取消通知,而無需遍歷已經安排的通知。如果你想要的話,你還可以通過鍵對象方法將Id標籤添加到通知中。

0

無論在哪個類,代碼將爲,它使用的任何地方的UIApplication單... 可以使用取消所有通知:

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

如果你想刪除特定的通知,您可以使用用戶信息的通知對象,當你創建一個本地通知時,爲其添加一個唯一的ID。稍後,您可以使用該ID來刪除本地通知。

對於可以使用以下代碼:

NSString *notificationId = @"id_to_cancel"; 
UILocalNotification *notification = nil; 
for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications]) 
{ 
    if([notify.userInfo objectForKey:@"ID"] isEqualToString:notificationId ]) 
    { 
    notification = notify; 
    break; 
    } 
} 
[[UIApplication sharedApplication] cancelLocalNotification:notification]; 
+0

它是crashing.The錯誤是「試圖插入從對象[0]'零對象」。 – iworld 2014-12-08 07:13:53

+0

這是因爲您需要使用ID創建本地通知。 – gronzzz 2014-12-08 12:11:21

相關問題