How I can set a notification which UserNotifications Framework

后端 未结 1 1276
故里飘歌
故里飘歌 2020-12-20 06:49

I can set a notification in a time interval but I don\'t know how make it in a specific time and date, I try this but don\'t work

let center = UNUserNotific         


        
相关标签:
1条回答
  • 2020-12-20 07:13

    You're almost there. Not much left for you to do.

    What you are missing is as follow:

    • You should add weekday to your date components, 1 is for Sunday so in your case you will need to set it to 2 for the Monday notification and 6 for your Friday notification
    • If you need it to repeat you need to set the parameter repeats to true in your UNCalendarNotificationTrigger.

    Here is a example.

    // 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)
    
    0 讨论(0)
提交回复
热议问题