在特定日期之前设置localNotification日

时间:2014-04-07 16:19:54

标签: ios uilocalnotification

我正在开发一个应用程序,它会向用户发送一个localNotification,提醒他们某个鞋子正在推出。

我认为通知应该在前一天晚上8点发送。

这是我的日期字符串(名为 releaseDate ):

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

如何在鞋子发布前一天晚上8点向用户发送localNotification?

- (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];
}

感谢。

3 个答案:

答案 0 :(得分:3)

将日期减少到一天,例如

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

编辑:另一种方式

NSCalendar * calendar = [NSCalendar currentCalendar];

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

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

终于解决了如何获得昨天晚上8点的日期

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);

答案 1 :(得分:0)

您可以从日期中扣除一天:

NSDate *reminderDate = [self.datePicker date];

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

请注意,daysToRemove为负数。使用正数来增加天数。

答案 2 :(得分:0)

要从日期字符串中获取日期,您可以使用以下内容:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];
NSDate *reminder = [dateFormatter dateFromString:releaseDate];