【问题标题】:UILocalNotification with NSDate带有 NSDate 的 UILocalNotification
【发布时间】:2012-04-07 17:14:28
【问题描述】:

我通读了 UILocalNotification 的文档,示例显示使用 NSDateComponents,然后设置周、日、月等以安排通知。我基本上是在尝试做类似于每天在特定时间重复的闹钟。我在 UIPickerView 有一段时间,我从那个时候为我的对象创建了一个 NSDate。所以我抓紧时间,尝试像这样安排通知:

    NSDate *date = [dateFormatter dateFromString:s];
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSLog(@"%@", s);
    UILocalNotification *note = [[UILocalNotification alloc] init];
    note.alertAction = alarm.name;
    note.alertBody = alarm.note;
    note.fireDate = date;
    note.timeZone = [[NSCalendar currentCalendar] timeZone];
    note.repeatInterval = NSDayCalendarUnit;
                             note.timeZone = [NSTimeZone systemTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:note];

我可以这样做吗?还是我必须使用 NSDateComponents。我的代码目前没有触发通知,我想知道是否有可能以这种方式触发通知,或者您是否必须使用 NSDateComponents。

另外,如果我必须使用 NSDateComponents,我如何才能获得当前日期,以便我可以设置那天的闹钟,并且我假设从那里开始,repeatInterval 将在接下来的几天生效。

非常感谢!

【问题讨论】:

    标签: iphone uilocalnotification


    【解决方案1】:

    只要firedate 是一个有效的NSDate,就会触发一个通知。这是我在 App Time 中使用的代码

            UILocalNotification *localNotification = [[UILocalNotification alloc] init]];
    
            localNotification.fireDate = [NSDate date];
    
    
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.alertBody = @"some string";
            localNotification.repeatInterval = NSWeekCalendarUnit;
            localNotification.repeatCalendar = [NSCalendar currentCalendar];
            localNotification.soundName = UILocalNotificationDefaultSoundName;
    
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    

    检查您传递给 firedate 的日期是否为有效的 NSDate。我不确定您使用的时区重复代码是否需要?这可能会导致问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      相关资源
      最近更新 更多