【问题标题】:UILocalNotification end dateUILocalNotification 结束日期
【发布时间】:2013-08-06 10:35:24
【问题描述】:

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

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

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

【问题讨论】:

    标签: ios objective-c notifications uilocalnotification nscalendar


    【解决方案1】:

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

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

    【讨论】:

    • ...并在检查后决定是否应取消通知。这个答案不是我想听到的,但现在我确定我无法完成这个。谢谢@rckoenes
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      Use following code:
      
      UILocalNotification *localNotification = [[UILocalNotification alloc] init];
      localNotification.repeatInterval = NSDayCalendarUnit;
      

      【讨论】:

      • 我知道如何管理repeatInterval 财产,这不是我要问的。
      猜你喜欢
      • 2012-08-19
      • 1970-01-01
      • 2021-01-11
      • 2018-06-01
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多