Local Notification repetation at different times

丶灬走出姿态 提交于 2020-01-03 02:56:38

问题


I have an app that generates prayer times (5 times a day) i want to create notfication for the 5 prayers but the issue is the times change everyday based on some calculations.

Edit:

The calculations are done based on GPS location so when the user changes to another city the times will be updated accordingly. I input the date,timezone, GPS coordinates into a method and I get prayer time values in (HH:mm) format for that given day/location. Now I need to setup the Notfications. I'm not sure where to set them up.

here is the code

#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

回答1:


You can use the repeatInterval parameter to repeat your five notifications, making them appear at the same time every day. Unfortunately there's no way to adjust the time without running your app.

You can run a GPS app in the background, though that would be quite a drain on the battery just for setting some timers. (This background process is really designed for GPS tracker apps. I'm not sure what Apple would make of using it for a slightly different purpose.)

But the easiest way would be just to update when the app is launched. When it launches you would get the current notifications (using the scheduledLocalNotifications property of UIApplication), cancel them if they're incorrect or out of date and create new ones. Each notification has a dictionary payload that you can use to make it easier to identify your alarms.




回答2:


I had the same issue. Have a look at this thread (https://stackoverflow.com/a/56533797/5806009), this is how I resolved it.



来源:https://stackoverflow.com/questions/9862261/local-notification-repetation-at-different-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!