Trigger notification weekly Swift 3

后端 未结 1 1016
一整个雨季
一整个雨季 2020-11-28 16:31

I am trying to make a schedule, in which I need to remember all the weeks I have class such as Monday at certain time. The problem is that if I assign weekday = 1 (Sunday) w

相关标签:
1条回答
  • 2020-11-28 16:51

    You can set your trigger to repeat every monday at 1:05am as follow:

    import UserNotifications
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: DateComponents(hour: 1, minute: 5, weekday: 2), repeats: true)
    print(trigger.nextTriggerDate() ?? "nil")
    
    let content = UNMutableNotificationContent()
    content.title = "title"
    content.body = "body"
    // make sure you give each request a unique identifier. (nextTriggerDate description)
    let request = UNNotificationRequest(identifier: "identify", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request) { error in
        if let error = error {
            print(error)
            return
        }
        print("scheduled")
    }
    

    Don't forget to ask the user permission to schedule notifications before trying to schedule your notification:

    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) { granted, error in
        if granted {
            print("authorized")
        }
    }
    
    0 讨论(0)
提交回复
热议问题