【问题标题】:iPhone : Daily local notificationsiPhone : 每日本地通知
【发布时间】:2012-07-03 01:39:29
【问题描述】:

我正在尝试实现本地通知

这是我设置的

    // Current date
    NSDate *date = [NSDate date]; 

    // Add one minute to the current time
    NSDate *dateToFire = [date dateByAddingTimeInterval:20];

    // Set the fire date/time
    [localNotification setFireDate:dateToFire];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

我想设置一个固定时间(每天)开始推送,而不是 20 个。

例如:我想在每天早上 6:00 弹出一个推送通知。

怎么办?

谢谢

【问题讨论】:

    标签: ios objective-c iphone notifications uilocalnotification


    【解决方案1】:

    您只需要正确创建一个NSDate 对象作为您的开火日期(时间)。不要使用[NSDate dateByAddingTimeInterval: 20],而是使用如下内容:

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay: 3];
    [components setMonth: 7];
    [components setYear: 2012];
    [components setHour: 6];
    [components setMinute: 0];
    [components setSecond: 0];
    [calendar setTimeZone: [NSTimeZone defaultTimeZone]];
    NSDate *dateToFire = [calendar dateFromComponents:components];
    

    Here are the Apple NSDateComponents API docs

    然后当您将日期添加到通知时,将重复间隔设置为一天:

    [localNotification setFireDate: dateToFire];
    [localNotification setTimeZone: [NSTimeZone defaultTimeZone]];
    [localNotification setRepeatInterval: kCFCalendarUnitDay];
    

    与所有与日期相关的代码一样,如果您的时区使用夏令时,请务必在切换到夏令时期间测试其工作原理。

    【讨论】:

    • 在控制台上我试图找到火的日期,这是“2012-07-03 00:30:00 +0000”我不确定,这会每天火吗?
    • @iscavengers,如果您将重复间隔设置为kCFCalendarUnitDay,它将每天触发,如我在上面的代码中所示
    • 如何将其翻译成 SWIFT?
    • @YestayMuratov,这是一个老问题。我建议您提出一个新问题with the Ask button,指定您需要 Swift 的答案。谢谢。
    • 如果我想每天用 3 次不同时间烧它怎么办?
    【解决方案2】:

    我猜你需要的是 NSDayCalendarUnit

    您可以查看this的答案。而here 是另一个值得一读的教程。

    【讨论】:

      【解决方案3】:
       NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:10];
          UIApplication* app = [UIApplication sharedApplication];
      
          UILocalNotification* notifyAlarm = [[[UILocalNotification alloc] init] autorelease];
          if (notifyAlarm)
          {
              notifyAlarm.fireDate = alertTime;
              notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
              notifyAlarm.repeatInterval = 0;
              notifyAlarm.soundName = @"Glass.aiff";
              notifyAlarm.alertBody = @"Staff meeting in 30 minutes";
      
              [app scheduleLocalNotification:notifyAlarm];
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多