本地通知在不同时间重复

时间:2012-03-25 17:18:53

标签: ios uilocalnotification

我有一个应用程序可以产生祷告时间(每天5次)我想为5个祷告创建notfication,但问题是每天根据一些计算改变时间。

修改

计算基于GPS位置进行,因此当用户切换到另一个城市时,时间将相应更新。我将日期,时区,GPS坐标输入到方法中,我得到了给定日期/位置的(HH:mm)格式的祷告时间值。现在我需要设置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.
    // e.g. 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

2 个答案:

答案 0 :(得分:2)

您可以使用repeatInterval参数重复五个通知,使其每天在同一时间出现。不幸的是,如果不运行你的应用程序,就无法调整时间。

可以在后台运行一个GPS应用程序,但这对于设置一些计时器来说会耗费电池电量。 (这个后台进程实际上是为GPS跟踪器应用程序设计的。我不确定Apple会将它用于稍微不同的目的。)

但最简单的方法就是在应用启动时更新。当它启动时,您将获得当前通知(使用scheduledLocalNotifications的{​​{1}}属性),如果它们不正确或已过期则取消它们并创建新的通知。每个通知都有一个字典有效负载,您可以使用它来更轻松地识别警报。

答案 1 :(得分:0)

我有同样的问题。看看这个线程(https://stackoverflow.com/a/56533797/5806009),这就是我的解决方法。