本地通知在不同的时间重复

我有一个应用程序,产生祈祷时间(一天5次),我想创build通知5祈祷,但问题是每天的时代变化,根据一些计算。

编辑:

计算是基于GPS定位完成的,所以当用户更换到另一个城市时,相应地更新时间。 我inputdate,时区,GPS坐标到一个方法,我得到的礼拜时间值(HH:毫米)格式为给定的一天/位置。 现在我需要设置Notfications。 我不确定在哪里设置它们。

这里是代码

#import "PrayerTimeViewController.h" #import "PrayTime.h" @implementation PrayerTimeViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization UITabBarItem *tbi = [self tabBarItem]; [tbi setTitle:NSLocalizedString(@"PrayerTimes", nil)]; UIImage *i = [UIImage imageNamed:@"11-clock"]; [tbi setImage:i]; [i release]; } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"Madinah"]]; self.view.backgroundColor = background; [background release]; locationManager = [[CLLocationManager alloc]init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager setDistanceFilter:kCLDistanceFilterNone]; [locationManager startUpdatingLocation]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow]; if (t < -180) { return; } PrayTime *prayerTime = [[PrayTime alloc]init]; [prayerTime setCalcMethod:0]; [prayerTime setFajrAngle:16]; [prayerTime setIshaAngle:14]; [prayerTime setAsrMethod:0]; NSDate *curentDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* compoNents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:curentDate]; CLLocationCoordinate2D currLoc = [newLocation coordinate]; NSMutableArray *prayerCal = [prayerTime getDatePrayerTimes:[compoNents year] andMonth:[compoNents month] andDay:[compoNents day] andLatitude:currLoc.latitude andLongitude:currLoc.longitude andtimeZone:[[NSTimeZone localTimeZone] secondsFromGMT]/3600]; [prayerTime release]; [fajer setText:[prayerCal objectAtIndex:0]]; // UILocalNotification *localNotification = [[UILocalNotification alloc] init]; NSString *time = [prayerCal objectAtIndex:0]; NSString *dates = [NSString stringWithFormat:@"%d-%d-%d %@",[compoNents year],[compoNents month],[compoNents day],time]; NSDateFormatter *dateText = [[NSDateFormatter alloc]init]; [dateText setDateFormat:@"yyyy-MM-dd HH:mm"]; [dateText setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:[[NSTimeZone localTimeZone] secondsFromGMT]]]; NSLog(@"%@",[dateText dateFromString:dates]); [shrooq setText:[prayerCal objectAtIndex:1]]; [duhur setText:[prayerCal objectAtIndex:2]]; [aser setText:[prayerCal objectAtIndex:3]]; [maghreb setText:[prayerCal objectAtIndex:5]]; [isha setText:[prayerCal objectAtIndex:6]]; [prayerCal release]; } @end 

您可以使用repeatInterval参数重复您的五个通知,使它们每天同时出现。 不幸的是,没有运行你的应用程序,没有办法调整时间。

可以在后台运行一个GPS应用程序,虽然这将是相当耗费电池只是为了设置一些定时器。 (这个背景过程实际上是为GPS跟踪器应用程序devise的,我不确定苹果会用它来做一个稍微不同的目的)。

但最简单的方法就是在应用程序启动时更新。 当它启动时,你会得到当前的通知(使用UIApplicationscheduledLocalNotifications属性),如果它们不正确或过时并且创build新的,则取消它们。 每个通知都有一个字典有效载荷,您可以使用它来更容易地识别您的警报。