Как я могу установить уведомление, которое UserNotifications Framework

Я могу установить уведомление в интервале времени, но я не знаю, как сделать это в определенное время и дату, я пытаюсь это сделать, но не работает

let center = UNUserNotificationCenter.current()

func notificationSender(){

    center.requestAuthorization([.sound, .alert]) {
        (granted, error) in
        // We can register for remote notifications here too!
    }

    var date = DateComponents()
    date.hour = 13
    date.minute = 57

    let trigger = UNCalendarNotificationTrigger.init(dateMatching: date , repeats: false)

    let content = UNNotificationContent()
    // edit your content

    let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)

center.add(уведомление) }

уведомление необходимо повторять каждый понедельник и пятницу в 15:00


person danielpp95    schedule 07.07.2016    source источник


Ответы (1)


Ты почти там. Тебе не так много осталось сделать.

То, что вам не хватает, выглядит следующим образом:

  • Вы должны добавить weekday к своим компонентам даты, 1 для воскресенья, поэтому в вашем случае вам нужно будет установить его на 2 для уведомления в понедельник и 6 для уведомления о пятнице.
  • Если вам нужно, чтобы это повторялось, вам нужно установить параметр repeats на true в вашем UNCalendarNotificationTrigger.

Вот пример.

// Create date components that will match each Monday at 15:00
var dateComponents = DateComponents()
dateComponents.hour = 15
dateComponents.minute = 0
dateComponents.weekday = 2 // Monday

// Create a calendar trigger for our date compontents that will repeat
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
                                            repeats: true)

// Create the content for our notification
let content = UNMutableNotificationContent()
content.title = "Foobar"
content.body = "You will see this notification each monday at 15:00"

// Create the actual notification
let request = UNNotificationRequest(identifier: "foo",
                                    content: content,
                                    trigger: trigger)

// Add our notification to the notification center
UNUserNotificationCenter.current().add(request)
person aross    schedule 15.10.2016