2012-04-18 56 views
1

我想显示今天的日期与应用程序的徽章号​​码,这个日期应该在应用程序触发本地通知时更新,但问题是本地通知不会更新日期!并只显示我的项目创建日期!这里是我的代码:正在更新本地通知BadgeNumber

- (void) viewDidLoad { 

[self notification]; 

} 

    - (void) notification { 


     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
     NSDate *now = [NSDate date];  
     NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit) fromDate: now]; 

     [componentsForFireDate year]; 
     [componentsForFireDate month]; 
     [componentsForFireDate day]; 
     [componentsForFireDate setHour:1]; 
     [componentsForFireDate setMinute:2]; 
     [componentsForFireDate setSecond:1]; 

     NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate]; 

       UILocalNotification *notification = [[UILocalNotification alloc]init]; 
       notification.fireDate = fireDateOfNotification; 
       notification.timeZone = [NSTimeZone localTimeZone];     notification.repeatInterval= NSDayCalendarUnit; 


       NSString *date = [self showGregorianFullDate]; 

       notification.alertAction = @"View"; 
       notification.soundName = UILocalNotificationDefaultSoundName; 


     //updating badge number : 

       NSCalendar* Calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
       NSDateComponents *Components = [Calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)fromDate:[NSDate date]]; 

       int a = [Components day]; 
       notification.applicationIconBadgeNumber = a; 

      [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 

    } 
+0

它必须告诉你日期,当你执行这个代码时,我的意思是安排通知。也许,你只在那个时间安排了一次? – 2012-04-18 11:02:59

+0

通知每天都会更新日期! ,我每25小时安排一次通知!获取当前日期 – 2012-04-18 11:35:57

+0

您是否找到了解决方案?我想在某些应用程序中实现同样的想法..谢谢 – hafedh 2012-06-17 15:07:51

回答

1

您是否只安排这一个通知?当通知触发时,他们无法运行任何代码。如果您的应用已打开,您可以处理该通知,并且如果用户对您的通知采取了处理措施,您将再次有机会处理该通知。

此通知已设定为将徽章号码设定为某一天的号码。这个数字是一个固定的数字。通知被触发后它不会自动更改。

应用程序徽章旨在显示一些未处理的通知(as per the Human Interface Guidelines),因此可能不是显示日期的最佳位置。另外,如果您查看app store review guidelines,则可能会从应用商店拒绝任何以“人机界面准则”中未描述的方式使用系统提供的项目的应用程序。

如果你继续沿着这条路,那么你可能想看看Local Notification Programming Guide。它显示每个应用程序可以安排64个本地通知,并且您需要每天安排一个更新徽章号码到第二天。这意味着如果用户在65天内未打开您的应用程序,则徽章号码将会出错,您也不会有本地通知留给用户提醒。

-1

刚刚得到componentsForFireDate一天你notification.applicationIconBadgeNumber,就像这样......

//updating badge number : 
     int a = [componentsForFireDate day]; 
     notification.applicationIconBadgeNumber = a; 

的事情是,applicationIconBadgeNumber具有在创建通知的时间是预先确定的。如果您从[NSDate date]获得一天,它将显示您创建通知的那一天,而不是通知触发的那一天。