本地通知 - 在swift 3中重复间隔

时间:2017-01-23 06:08:58

标签: ios swift swift3 ios10 localnotification

我想每周重复本地通知,在iOS10之前有repeatInterval,但我无法找到任何适合在iOS10中重复通知的内容。 TimeTriggercalendarTrigger都重复为真或假,我可以在每周,每日,每月重复申请。

感谢。

1 个答案:

答案 0 :(得分:2)

试试这个。

func scheduleNotification(at date: Date, body: String) {     
    let triggerWeekly = Calendar.current.dateComponents([.weekday,hour,.minute,.second,], from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)

    let content = UNMutableNotificationContent()
    content.title = "Dont Forget"
    content.body = body
    content.sound = UNNotificationSound.default()
    //content.categoryIdentifier = "todoList"

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().delegate = self
    //UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
      if let error = error {
        print("Uh oh! We had an error: \(error)")
      }
    }
  }