ios - 在某个日期之前设置 localNotification 日期

标签 ios uilocalnotification

我正在开发一款应用,该应用会向用户发送本地通知,提醒他们某双鞋即将上市。

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

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

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

如何在鞋子发布前一天晚上 8:00 向用户发送本地通知?

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

谢谢。

最佳答案

例如,将您的日期减少到一天

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

关于ios - 在某个日期之前设置 localNotification 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22917809/

相关文章:

ios - 在 didReceiveLocalNotification 中以编程方式关闭 UIAlertview

ios - swift 弹出本地通知,没有角标(Badge)

ios - CBCentralManager iOS10 和 iOS9

ios - 如何理解 count number sqlite 行数?

swift - 在特定时间和同一时间后每隔一段时间重复本地通知

iphone - 本地通知在 ios 中运行不正常

ios - Local Notification 什么时候EnterRegion?

ios - 从 App Delegate 调用 ViewController 方法

ios - Xamarin:NSMutableUrlRequest 和 NSUrlRequest 与 POST 方法和标题和数据?

ios - 为什么导航栏在 UIViewWeb 之上? (OBJ-C 初学者)