在ios7中实现特定日期的本地通知

时间:2014-05-23 09:41:21

标签: ios objective-c ios7

我有两个表视图,每个index都有不同的NSMutableArray和字典.dictionary有一个NSString格式的日期字段。现在我的问题是当用户使用选择器视图设置时间时,如果当天有可用记录,那么他同时获得local notification的每一天。 我不知道如何实现这个请帮帮我.. 谢谢

2 个答案:

答案 0 :(得分:0)

在您的情况下,您需要设置fireDate它是您的通知到达的日期/时间,并设置repeatInterval它描述您的通知重复。在这里,您需要设置kCFCalendarUnitDay,因为您想每天重复通知。

  

REPEAT UNIT:

     

NSEraCalendarUnit
  NSYearCalendarUnit
  NSMonthCalendarUnit
  NSDayCalendarUnit
  NSHourCalendarUnit
  NSMinuteCalendarUnit
  NSSecondCalendarUnit
  NSWeekCalendarUnit
  NSWeekdayCalendarUnit
  NSWeekdayOrdinalCalendarUnit
  NSQuarterCalendarUnit

有关更多信息,有一些SO的问题。

Repeat UILocalNotification daily at 5 pm
iPhone : Daily local notifications
http://useyourloaf.com/blog/2010/09/13/repeating-an-ios-local-notification.html

答案 1 :(得分:0)

在AppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
  return YES;
 }

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{    
  UIAlertView *notificationAlert = [[UIAlertView alloc] initWithTitle:@"Notification"    message:@"This local notification" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
  [notificationAlert show];
   // NSLog(@"didReceiveLocalNotification");
}

将此代码放在ViewController的.m文件中:

-(IBAction)startLocalNotification 
{  
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"h:mm a"];
  NSDate *dateFromString = [[NSDate alloc] init];
  dateFromString = [dateFormatter dateFromString:timeStr];

  NSLog(@"startLocalNotification");    
  UILocalNotification *notification = [[UILocalNotification alloc] init];
  notification.fireDate = dateFromString;
  notification.repeatInterval = NSDayCalendarUnit;
  notification.alertBody = @"This is local notification!";
  notification.timeZone = [NSTimeZone defaultTimeZone];
  notification.soundName = UILocalNotificationDefaultSoundName;
  notification.applicationIconBadgeNumber = 10;
  [[UIApplication sharedApplication] scheduleLocalNotification:notification];    
}
相关问题