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 = 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(notification)
}
the notification need to be repeated every Monday and Friday at 3 pm
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)