本地通知在不同时间重复

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (5)

我有一个应用程序,可以产生祈祷时间(每天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
提问于
用户回答回答于

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

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

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

所属标签

可能回答问题的人

  • 西风

    renzha.net · 站长 (已认证)

    9 粉丝1 提问5 回答
  • uncle_light

    5 粉丝518 提问3 回答
  • 学生

    2 粉丝477 提问3 回答
  • 军哥

    重庆雷驰信息技术有限公司 · 经理 (已认证)

    5 粉丝1 提问3 回答

扫码关注云+社区

领取腾讯云代金券