How I can set a notification which UserNotifications Framework

混江龙づ霸主 提交于 2019-11-29 12:30:51

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