【问题标题】:How do I get next fire date from a local notification..?如何从本地通知中获取下一个开火日期..?
【发布时间】:2013-09-05 12:40:12
【问题描述】:

我正在使用UILocalNotifications 实现一个提醒应用程序,并希望允许用户设置重复模式。对于重复模式使用NSDayCalenderUnitNSWeekCalendarUnit等。

我没有看到 LocalNotification 类中的任何属性来从通知中获取下一个触发日期。

示例

"{开火日期 = 星期五, 2013 年 9 月 6 日,上午 7:05:00 印度标准时间,时区 = 亚洲/加尔各答 (GMT+05:30) 偏移 19800,重复间隔 = NSWeekCalendarUnit,重复计数 = UILocalNotificationInfiniteRepeatCount,下一个触发日期 = 星期五, 2013 年 9 月 6 日,上午 7:05:00 印度标准时间,用户信息 = {\n
kRemindMeNotificationDataKey = \"测试通知\";\n}}"

请任何人帮助我从本地通知中获取下一个开火日期。

提前致谢。

【问题讨论】:

    标签: ios uilocalnotification


    【解决方案1】:

    您有两个来自UILocalNotification 的属性可供您使用:

    1. 开火日期
    2. 重复间隔

    根据这两个属性创建一个 NSDate。

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:self.localNotification.repeatInterval fromDate:self.localNotification.fireDate];
    
    NSDate *nextFireDate = [calendar dateFromComponents:comps];
    

    【讨论】:

    • 谢谢,其实我是本地通知的新手。我添加了一个具有周重复模式的任务,将重复间隔设为 256。那么我如何使用该间隔和 fireDate 创建下一个触发日期。
    • fireDate: 2013-09-06 13:34:00 +0000 repeatInterval: 16 (NSDayCalendarUnit) 我得到 0001-01-05 18:06:32 +0000 而不是 2013-09-07 13:34:00 +0000
    • 您不需要创建下一个开火日期。在重复模式下,它使用来自 repeatInterval 的 NSCalendarUnit 自动创建下一个触发日期。我写的答案是让你知道你是否明确需要它。否则要触发 LocalNotifications,您不需要在重复模式的情况下再给它一个 fireDate
    • 实际上我想在我的列表屏幕上向用户显示下一个 fireDate。所以我需要计算一下。
    • 通过使用上面提到的.two 属性,您可以计算thr next ate 并显示它。我已经向你展示了如何做到这一点。试试看。如果它不起作用,请告诉我。
    【解决方案2】:

    试试这个代码

    NSCalendar *calendar = notification.repeatCalendar;
    components.week = 1;
             if (!calendar) {
                 calendar = [NSCalendar currentCalendar];
             }            
    NSDate *nextFireDate = [calendar dateByAddingComponents:components toDate:notification.fireDate options:0];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      相关资源
      最近更新 更多