Khushboo
Khushboo

Reputation: 181

Local notifications - repeat Interval in swift 3

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.

Upvotes: 1

Views: 1698

Answers (1)

Ganesh Kumar
Ganesh Kumar

Reputation: 1651

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

Upvotes: 2

Related Questions