2014-04-24 56 views
0

这是我的localNotification代码。关于NSLocalNotifications的几个问题

NSDate *currentDate = [NSDate date]; 
     NSCalendar * calendar = [NSCalendar currentCalendar]; 
     [calendar setTimeZone:[NSTimeZone systemTimeZone]]; 
     NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:currentDate]; 

     [components setTimeZone:[NSTimeZone systemTimeZone]]; 
     [components setHour:13]; 
     [components setMinute:11]; 
     [components setDay: sonOdemeGunuNumber.integerValue -3]; 

     NSDate *test = [calendar dateFromComponents:components]; 


     // Schedule the notification 
     UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
     localNotification.fireDate = test; 
     localNotification.alertBody = [NSString stringWithFormat:@"%@ 3 days left.",_txtBankaAdi.text]; 
     localNotification.alertAction = @"Show item"; 
     localNotification.timeZone = [NSTimeZone systemTimeZone]; 
     localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
     localNotification.repeatInterval = kCFCalendarUnitDay; 
     localNotification.repeatInterval = kCFCalendarUnitMonth; 

     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

sonOdemeGunuNumber是ToDoItem警报日(当前日期的数量,例如-3)。

if(sonOdemeGunuNumber.integerValue == currentDay.integerValue){ 
    localNotification.repeatInterval = nil; } 

我想3天前通知用户,并应继续得到警报,直到CURRENTDAY = sonOdemeGunuNumber。

1-如果我写这个if-else语句,它会起作用吗?

2-如果用户删除我的TableView中的localNotification项目,在这种情况下,项目是银行账户? 银行帐户localNotification会自动删除自己吗?

谢谢! 有一个愉快的一天每个人都...

回答

0

我发现,如果用户删除的tableview我取消已删除项目的localNotification项目的解决方案..

首先,随着用户信息发送ID到LocalNotification中心。

localNotification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:_txtItemName.text,@"delThisNot", nil]; 

其次去编辑你的tableView方法。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

{ 如果(editingStyle == UITableViewCellEditingStyleDelete){

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; 
     NSString *adi = [[self.fetchedResultsController objectAtIndexPath:indexPath]valueForKey:@"odemeAdi"]; 
     NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"delThisNot"]]; 

     if ([uid isEqualToString:adi]) 
     { 
      //Cancelling local notification 
      [app cancelLocalNotification:oneEvent]; 
      break; 
     } 
    } 
} 

ADI =删除项目。 的uid =您的产品ID(这是你的txtItemName.text)

if ([uid isEqualToString:adi]) 
     { 
      //Cancelling local notification 
      [app cancelLocalNotification:oneEvent]; 
      break; 
     } 

如果用户想要删除的项目名称(ADI)== UID(txtItemName.Text)

取消此通知。

对不起,我grammers我只是想帮助其他人,我希望你们能理解我:d

感谢。