1

我想UILocalNotification在特定日期触发 a。如果我使用此代码:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit fromDate:[NSDate date]];

[components setHour:4];
[components setMinute:0];

NSDate *fireDate = [gregorian dateFromComponents:components];

如果现在是下午 3 点,这可以正常工作,但在例如下午 5 点时就不行了。

如何将通知触发日期设置为“下一个下午 4 点”?

4

3 回答 3

4
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit fromDate:[NSDate date]];

[components setHour:4];
[components setMinute:0];

NSDate *fireDate = [gregorian dateFromComponents:components];
 NSLog(@"Fire date : %@",fireDate);// Today 4pm is already passed. So you wont get notification 

// You need to check if the time is already passed
if ([fireDate compare:[NSDate date]] == NSOrderedAscending)
{
    // Schedule it for next day 4pm
    components.day = components.day+1;
    fireDate = [gregorian dateFromComponents:components];
}
NSLog(@"Fire date : %@",fireDate);
于 2013-03-26T13:22:27.707 回答
1

尝试以下 24 小时格式的代码:-

NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour:16];
[components setMinute:0];
[components setSecond:0];

NSDate *newDate = [gregorian dateFromComponents: components];
[gregorian release];    

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = newDate;
localNotif.alertBody = [NSString stringWithFormat:@"Alarm for time : %@",newDate];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
于 2013-03-26T12:15:30.440 回答
0

swift 3 - 4中,您可以将其用于特定时间的本地通知。

  func scheduleLocal() {

        let dateComponents = DateComponents(year: 2019, month: 1, day: 17, hour: 10, minute: 20)
        let yourFireDate = Calendar.current.date(from: dateComponents)

        let notification = UILocalNotification()
        notification.fireDate = yourFireDate
        notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
        notification.alertAction = "be awesome!"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.userInfo = ["CustomField1": "w00t"]
        UIApplication.shared.scheduleLocalNotification(notification)

        guard let settings = UIApplication.shared.currentUserNotificationSettings else { return }

        if settings.types == .none {
            let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            present(ac, animated: true, completion: nil)
            return
        }

    }
于 2019-01-17T07:20:50.837 回答