Local notifications - repeat Interval in swift 3

无人久伴 提交于 2019-12-14 02:25:30

问题


I want to repeat local notification every week, before iOS10 there is repeatInterval, but i am not able to find anything suitable to repeat notifications in iOS10. TimeTrigger and calendarTrigger both have repeat as true or false, where can i apply repeat as weekly, daily, monthly.

Thanks.


回答1:


Try this.

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)")
      }
    }
  }


来源:https://stackoverflow.com/questions/41800189/local-notifications-repeat-interval-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!