【问题标题】:Repeating local notification daily at a set time with swift每天在设定的时间快速重复本地通知
【发布时间】:2015-06-03 12:23:17
【问题描述】:

我是 iOS 开发的新手,但已经创建了应用程序,并且我正在尝试在设定的时间创建每日通知。目前,通知在给定的日期/时间执行一次。我不确定如何使用 repeatInterval 方法来每天安排它。每天重复通知的最佳方法是什么?任何帮助将不胜感激(Y)。

    var dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015;
    dateComp.month = 06;
    dateComp.day = 03;
    dateComp.hour = 12;
    dateComp.minute = 55;
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    var date:NSDate = calender.dateFromComponents(dateComp)!

    var notification:UILocalNotification = UILocalNotification()
    notification.category = "Daily Quote"
    notification.alertBody = quoteBook.randomQuote()
    notification.fireDate = date
    notification.repeatInterval = 

    UIApplication.sharedApplication().scheduleLocalNotification(notification)

【问题讨论】:

    标签: ios swift cocoa-touch uilocalnotification


    【解决方案1】:

    您必须提供 NSCalendarUnit 值,例如“HourCalendarUnit”或“DayCalendarUnit”才能重复通知。

    只需添加此代码即可每天重复本地通知:

    notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
    

    【讨论】:

    • 由于某种原因,通知会一个接一个地被推送。我将其更改为分钟以进行测试,但从那时起,即使我将其更改回来,通知也会继续推送。任何想法为什么? @vizllx
    • 我也有同样的问题。有没有人知道原因和解决方法
    • 如果一个接一个地推送,我刚刚发现在不同的安装会话中注册的本地通知仍然存在 - 如果您卸载然后重新安装应用程序,之前的通知(如果它们还没有触发) 如果在最新的安装会话中再次启用本地通知,将立即全部触发。希望这对其他人有帮助。
    • 请更新您对 Swift 3 和 Swift 4 的答案!对新的 iOS 开发者很有帮助
    【解决方案2】:

    因此,不得不稍微修改@vizllx 的上述代码。这是新的一行:

    notification.repeatInterval = NSCalendarUnit.Day 
    

    这是我使用的一个完整的工作示例:

    let notification = UILocalNotification()
    
      /* Time and timezone settings */
      notification.fireDate = NSDate(timeIntervalSinceNow: 8.0)
      notification.repeatInterval = NSCalendarUnit.Day
      notification.timeZone = NSCalendar.currentCalendar().timeZone
      notification.alertBody = "A new item is downloaded."
    
      /* Action settings */
      notification.hasAction = true
      notification.alertAction = "View"
    
      /* Badge settings */
      notification.applicationIconBadgeNumber =
      UIApplication.sharedApplication().applicationIconBadgeNumber + 1
      /* Additional information, user info */
      notification.userInfo = [
        "Key 1" : "Value 1",
        "Key 2" : "Value 2"
      ]
    
      /* Schedule the notification */
      UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }
    

    【讨论】:

      【解决方案3】:

      其他答案展示了如何使用旧的、iOS 10 之前的本地通知。对于 iOS 10 及更高版本,您必须按如下方式使用 UNNotificationCenter:

          let content = UNMutableNotificationContent()
          content.title = "This will appear in bold, on it's own line."
          content.subtitle = "This will appear in bold, on it's own line, below the title."
          content.body = "This will appear as normal text, below the title and subtitle."
      
          let triggerInputForHourlyRepeat = Calendar.current.dateComponents([.minute], from: intendedFireDateVariable)
          let trigger = UNCalendarNotificationTrigger.init(dateMatching: triggerInput, repeats: true)
      
          let request = UNNotificationRequest(identifier: "someUniqueID", content: content, trigger: trigger)
          let unc = UNUserNotificationCenter.current()
          unc.add(request, withCompletionHandler: { (error) in
              /// Handle error
          })
      

      有用的教程:

      1. https://www.techotopia.com/index.php/An_iOS_10_Local_Notification_Tutorial
      2. https://makeapppie.com/2016/11/21/manage-delete-and-update-notifications-in-ios-10/

      【讨论】:

      • 问题询问关于在特定时间触发每日通知。您的答案没有显示如何做到这一点。没有显示提供特定时间的代码。
      【解决方案4】:

      var repeatInterval: NSCalendarUnit

      => 文档说“重新安排通知的日历间隔。”

      所以:使用NSCalendarUnit.CalendarUnitDay

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        • 2016-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多