1

我想知道是否可以设置结束日期UILocalNotification

我希望我的通知每天触发(NSDayCalendarUnit),但我有一个我不能跨越的结束日期(截止日期),例如,我在一年的时间里每天都在拍摄我的胡须长的照片,一年后将不会显示通知。

希望你能理解我的观点...

4

3 回答 3

4

UILocalNotification您可以在文档中阅读到没有这样的选项。

您唯一的选择是在每个用户启动应用程序时检查这一年是否结束。

于 2013-08-06T10:38:17.767 回答
1

UILocalNotification对象中,我建议设置repeatInterval属性并将结束日期放入userInfo字典中,以便稍后查询以确定通知是否已过期。例如:

UILocalNotification* uiLocalNotification;
uiLocalNotification = [[UILocalNotification alloc] init]; 

//Set the repeat interval
uiLocalNotification.repeatInterval = NSCalendarUnitDay;

NSDate* fireDate = ...

//Set the fire date
uiLocalNotification.fireDate = fireDate;

//Set the end date
NSDate* endDate = ...

uiLocalNotification.userInfo = @{
                                  @"endDate": endDate
                                };

UIApplication* application = [UIApplication sharedApplication];
[application scheduleLocalNotification:uiLocalNotification];

//...

//Somewhere else in your codebase, query for the expiration and cancel, if necessary

UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = application.scheduledLocalNotifications;

NSDate* today = [NSDate date];

for (UILocalNotification* notification in scheduledNotifications) {

    NSDate* endDate = notification.userInfo[@"endDate"];

    if ([today earlierDate:endDate] == endDate) {
        //Cancel the notification
        [application cancelLocalNotification:notification];
    }
}
于 2015-06-26T22:00:31.970 回答
-3
Use following code:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSDayCalendarUnit;
于 2013-08-06T10:40:19.637 回答