简体   繁体   中英

Impliment local notification for specific day in ios7

I have two table view both has different NSMutableArray with dictionary at every index.dictionary has a date field in NSString format. Now my problem is when user set time with picker view than every day he got local notification at the same time if record available for that day. I have no idea how to implement this please help me.. Thank you

In your case you need to set fireDate its is date/time on which your notification will be arrive and also set repeatInterval it's describe repetition of your notification. Here you need to set kCFCalendarUnitDay because you want to repeat notification daily.

REPEAT UNIT:

NSEraCalendarUnit
NSYearCalendarUnit
NSMonthCalendarUnit
NSDayCalendarUnit
NSHourCalendarUnit
NSMinuteCalendarUnit
NSSecondCalendarUnit
NSWeekCalendarUnit
NSWeekdayCalendarUnit
NSWeekdayOrdinalCalendarUnit
NSQuarterCalendarUnit

For more information there are some SO's question.

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

In 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");
}

This code in .m file to your ViewController :

-(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];    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM