2013-07-22 54 views
0

下面是我的代碼,我有檢查互聯網一切似乎不錯,但不知道通知不點火,我知道它是如此簡單,但我仍然堅持在這裏請大家幫忙問題在本地通知不點火

-(void)notification:(NSMutableArray *)nameArray datearray:(NSMutableArray*)datearray; 

    { 
     NSLog(@"name%@",nameArray); 
     NSLog(@"date%@",datearray); 

     "Jodie Hession", 
    "John Miller", 

    "Shane Jarome", 
    "Sharon Scott", 
    "Sheron Begley", 
    "Susan Diaz", 
    Kate, 
    John, 
    Anna, 
    David, 
    today, 
    "After Some day" 
) 
date(
    "14/12/2013 04:35:00", 
    "18/12/2013 04:35:00", 
    "04/04/2014 04:35:00", 
    "01/01/2014 04:35:00", 
    "29/06/2014 04:35:00", 
    "05/01/2014 04:35:00", 
    "18/12/2013 04:35:00", 
    "20/01/2014 04:35:00", 
    "22/06/2014 04:35:00", 
    "29/08/2013 04:35:00", 
    "15/06/2014 04:35:00", 
    "22/07/2013 04:35:00", 
    "31/07/2013 04:35:00" 
) 


    // logic for local notification start 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 




    NSDateFormatter *Form=[[[NSDateFormatter alloc]init]autorelease]; 
    [Form setDateFormat:@"dd/MM/yyyy hh:mm:ss"]; 


    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    for (int i=0;i<datearray.count;i++) 
    { 
     NSDate *date =[Form dateFromString:[datearray objectAtIndex:i ]]; 

     NSLog(@"%@",date); 

     if(notification) 
     { 

      notification.fireDate = date; 

      //   if(notification.fireDate == date){ 
      // 
      //    notification.applicationIconBadgeNumber = 1; 
      // 
      //   } 


      notification.timeZone = [NSTimeZone defaultTimeZone]; 
      notification.alertBody = [NSString stringWithFormat:@"Today is %@\'s Birthday",[nameArray objectAtIndex:i]]; 
      notification.alertAction = @"View"; 

      notification.soundName = UILocalNotificationDefaultSoundName; 

      [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
     } 

     NSLog(@"fire date%@", notification.fireDate); 
    // fire date2013-07-30 23:05:00 +0000 

不要知道爲什麼,但NSDate的總是打印別的東西,如果我們用stringfromdate檢查它那麼只有它顯示了正確的價值 }

} 

,並在我的應用程序delegete

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 


    NSLog(@"Notification Received, %@, set for date %@", notification.alertBody, notification.fireDate); 
} 
+0

你檢查了最小化應用程序? –

+0

yesss我做了先生 – java

+0

盡我所知,通知不適用於循環。如果你運行一個值,它正在運行!讓我研究更多 –

回答

1

看起來這是您使用的日期格式問題。我在GMTsetDateFormat:@"yyyy-MM-dd HH:mm:ss Z"做了它,它現在工作正常。

下面是代碼爲你:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    NSMutableArray *datearray = [[NSMutableArray alloc]initWithObjects: 
          @"23/07/2013 12:33:00", 
          @"23/07/2013 12:33:05", 
          @"23/07/2013 12:33:10", 
          @"23/07/2013 12:33:15", 
          @"23/07/2013 12:33:20", 
          @"23/07/2013 12:33:25", 
          @"23/07/2013 12:33:30", 
          @"23/07/2013 12:33:35", 
          @"23/07/2013 12:33:40", 
          @"23/07/2013 12:33:45", 
          @"23/07/2013 12:33:50", 
          @"23/07/2013 12:33:55", 
          @"23/07/2013 12:34:00", 
          nil]; 
    NSMutableArray *nameArray = [[NSMutableArray alloc]initWithObjects: 
           @"Jodie Hession", 
           @"John Miller", 
           @"Shailesh Mistry", 
           @"Shane Jarome", 
           @"Sharon Scott", 
           @"Sheron Begley", 
           @"Susan Diaz", 
           @"Kate", 
           @"John", 
           @"Anna", 
           @"David", 
           @"today", 
           @"After Some day", 
           nil]; 
    [self notification:nameArray datearray:datearray]; 
} 

-(void)notification:(NSMutableArray *)nameArray datearray:(NSMutableArray*)datearray; 
{ 
    NSDateFormatter *Form = [[NSDateFormatter alloc]init]; 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    for (int i=0;i<datearray.count;i++) 
    { 
     //[Form setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; 
     //NSDate *date =[Form dateFromString:[datearray objectAtIndex:i]]; 
     [Form setDateFormat:@"dd-MM-yyyy HH:mm:ss"]; 
     NSDate *date =[Form dateFromString:[[datearray objectAtIndex:i] stringByReplacingOccurrencesOfString:@"/" withString:@"-"]]; 
     NSLog(@"%@",date); 
     if(notification) 
     { 
      // un-comment this for current date time 
      /*[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5*i]]; 
      NSLog(@" %@",[NSDate dateWithTimeIntervalSinceNow:5*i]);*/ 

      //comment this for current date time 
      [notification setFireDate:date]; 
      NSLog(@" %@",date); 

      //notification.fireDate = date; 
      //if(notification.fireDate == date){ 
      //} 

      notification.applicationIconBadgeNumber = i; 
      notification.timeZone = [NSTimeZone defaultTimeZone]; 
      notification.alertBody = [NSString stringWithFormat:@"Today is %@\'s Birthday",[nameArray objectAtIndex:i]]; 
      notification.alertAction = @"View"; 
      notification.soundName = UILocalNotificationDefaultSoundName; 
      [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
     } 

     NSLog(@"fire date%@", notification.fireDate); 
    } 
} 

爲了與當前的日期時間與5 sec間隔測試,可以if(notification)後取消註釋的前2行和評論未註釋行之後的行。

+0

先生,因爲我的日期formater是「20/01/2014 11:59:00」,我們嘗試改變它yyyy-MM-dd HH: mm:ss Z因爲那個日期變成了null如何處理這個? – java

+0

你從webservice獲取這些日期時間? –

+0

是的,每個地方都有一些來自Facebook的用戶和一些來自地址簿。那麼我們該如何改變這個formater?所以nsdate不會變爲空 – java