2013-03-16 143 views
0

在我的應用程序中,我有一個朋友的生日數組,我需要在某人的生日即將到來時通知用戶。 Array將有大約200位朋友的生日。在這種情況下,UILocalNotification或推送通知

UILocalNotification會解決與否,如蘋果所說Each application on a device is limited to the soonest-firing 64 scheduled local notifications.如果是,我該如何實施UILocalNotication。

我不想去PushNotification.任何意見,將不勝感激。

我安排本地通知如下: -

-(void) scheduleNotification{ 
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
    [self.notifications removeAllObjects]; 
    for (int i = 0; i< [delegate.viewController.contactList count] ; i++) { 
     UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
     NSString *name = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:NAME_KEY]; 
     NSString *birthday = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:BIRTHDAY_KEY]; 
     if (birthday) { 
      [formatter setDateFormat:@"MM/dd/yyyy"]; 
      [formatter setLocale:[NSLocale currentLocale]]; 
      [formatter setTimeZone:[NSTimeZone systemTimeZone]]; 
      NSDate *date = [formatter dateFromString:birthday]; 
      if (date == nil) { 
       [formatter setDateFormat:@"MM/dd"]; 
       [formatter setLocale:[NSLocale currentLocale]]; 
       [formatter setTimeZone:[NSTimeZone systemTimeZone]]; 
       date = [formatter dateFromString:birthday]; 
      } 
      NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
      NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date]; 

      componentsEnd.year = [[NSDate date] year]; 
      date = [gregorianEnd dateFromComponents:componentsEnd]; 
      self.alarmTime = [date dateByAddingTimeInterval:self.mTimeInterval]; 
      localNotification.fireDate = _alarmTime; 
      localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
      localNotification.applicationIconBadgeNumber = 1; 

      NSString *itemName = @"B'day Alert!!!"; 
      NSString *msgName = [NSString stringWithFormat:@"Celebrate %@'s B'day",name]; 
      NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:itemName,MessageKey, msgName,TitleKey, nil]; 
      localNotification.userInfo = userDict; 
      localNotification.soundName = self.soundName; 
      localNotification.alertBody = [NSString stringWithFormat:@"Celebrate %@'s B'day",name]; 


      [self.notifications addObject:localNotification]; 
      } 
     } 
    } 
} 

我已經實現了我的applicationDidEnterBackground代表爲:

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 

    UILocalNotification* notification; 

    for (int i = 0; i< [self.settingVC.notifications count]; i++) { 

     notification = [self.settingVC.notifications objectAtIndex:i]; 
     [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    } 

} 

此外,在didReceiveLocalNotification委託我有這樣的:

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    NSString *itemName = [notification.userInfo objectForKey:TitleKey]; 
    NSString *messageTitle = [notification.userInfo objectForKey:MessageKey]; 
    [self _showAlert:itemName withTitle:messageTitle]; 
    application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1; 
} 

我應該在上述功能中做些什麼改變。

回答

2

你仍然可以使用UILocalNotification並且只安排最接近的生日64。每當應用程序中有活動時重新安排這些時間,以便最新的更新。我會想象64個通知將允許用戶有足夠的時間,然後他們需要再次啓動應用程序。

+0

: - 我在我的問題中添加了我的代碼。我需要在代碼中進行哪些更改。 – iCoder4777 2013-03-16 20:10:27