Question

I'm working on an app that will send the user a localNotification reminding them that a certain shoe is coming out.

I'm thinking the Notification should be sent the day before at 8:00PM.

This is my Date string (called releaseDate):

2014-04-10 00:00:00 +0000

How can I send the User a localNotification at 8:00PM the day before the shoe is release?

- (IBAction)addReminder:(id)sender {
    // Get the current date
    NSDate *reminderDate = ??;

    // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = reminderDate;
    localNotification.alertBody = @"Hi";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

Thanks.

Was it helpful?

Solution

decrease your date to one day for example

[[NSDate date] dateByAddingTimeInterval:60*60*24*-1];

edit: another way

NSCalendar * calendar = [NSCalendar currentCalendar];

NSDateComponents * comps = [NSDateComponents new];
[comps setDay:-1];

NSDate * yesterdayDate = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSLog(@"%@", yesterdayDate);

finally solution how to get yesterday 8 pm date

NSDate * releaseDate = [NSDate date];
NSCalendar * calendar = [NSCalendar currentCalendar];

NSDateComponents * comps = [NSDateComponents new];
[comps setDay:-1];

NSDate * yesterdayDate = [calendar dateByAddingComponents:comps toDate:releaseDate options:0];
NSDateComponents * comps2 =  [calendar components:NSCalendarUnitDay|NSCalendarUnitMonth| NSCalendarUnitYear  fromDate: yesterdayDate];

[comps2 setHour:20];
[comps2 setMinute:0];

NSDate * yesterday8PM = [calendar dateFromComponents:comps2];
NSLog(@"%@", yesterday8PM);

OTHER TIPS

You can substract one day from your date:

NSDate *reminderDate = [self.datePicker date];

int daysToRemove = -1;
NSDate *notificationDate = [reminderDate dateByAddingTimeInterval:60*60*24*daysToRemove];

Note that the daysToRemove is negative. Use positive numbers to add days.

To get the date from your date string, you can use the following:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];
NSDate *reminder = [dateFormatter dateFromString:releaseDate];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top