简体   繁体   English

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

[英]How do I make NSUserNotification repeat daily in Mac application?

I want to create daily remind for my Mac application. 我想为我的Mac应用程序创建每日提醒。 I've set deliveryRepeatInterval, but it didn't work. 我设置了deliveryRepeatInterval,但是没有用。 It's only create 1 notification. 仅创建1条通知。 My code is below: 我的代码如下:

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

I have repaired my code, and it worked. 我已经修复了代码,并且可以正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM