使用swift在设定的时间每天重复本地通知

时间:2015-06-03 12:23:18

标签: ios swift cocoa-touch uilocalnotification

我是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)

4 个答案:

答案 0 :(得分:14)

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

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

notification.repeatInterval = NSCalendarUnit.CalendarUnitDay

答案 1 :(得分:13)

所以,不得不修改@ 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)
}

答案 2 :(得分:1)

var repeatInterval: NSCalendarUnit

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

所以:使用NSCalendarUnit.CalendarUnitDay

答案 3 :(得分:0)

其他答案显示了如何使用旧的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/