如何在Mac应用程序中每天重复NSUserNotification?

时间:2015-06-24 08:49:05

标签: objective-c macos

我想为我的Mac应用程序创建每日提醒。我已经设置了deliveryRepeatInterval,但它没有用。它只创建1个通知。 我的代码如下:

    NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSHourCalendarUnit) fromDate:date];
    components.timeZone = [NSTimeZone localTimeZone];
    [components setDay: components.day + 1];
    [components setHour: 20];
    NSDate *dateToFire = [calendar dateFromComponents:components];

    NSUserNotification *localNotif = [NSUserNotification new];
    localNotif.deliveryRepeatInterval.day = 1;
    localNotif.informativeText = @"Hello";
    localNotif.deliveryTimeZone = [NSTimeZone localTimeZone];
    localNotif.deliveryDate = dateToFire;

    NSUserNotificationCenter *notiCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
    [notiCenter scheduleNotification:localNotif];

1 个答案:

答案 0 :(得分:1)

我修复了我的代码,但它确实有效。

NSDateComponents *repeatTime = [NSDateComponents new];
repeatTime.day = 1;
localNotif.deliveryRepeatInterval = repeatTime;