Answer the question
In order to leave comments, you need to log in
How to make local notifications by day/hour in Swift?
Good day. I'm new to Swift, so I couldn't find the answer to my question on Google. Do not judge strictly.
Made an application with local notifications. I want the application to give out a notification with a NEW phrase every day.
How to make notifications by date? That is, so that on 18/02/20 at 18:54 the application would issue a phrase. Further 19:02:20 at 18:54 issued the following phrase.
How to make this type of notification, and how to "shove" in an easier way?
Answer the question
In order to leave comments, you need to log in
let content = UNMutableNotificationContent()
content.sound = .default
content.title = "Заголовок"
content.body = "Сообщение"
var dateComponents = DateComponents()
dateComponents.hour = 9
dateComponents.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UNNotificationRequest.weeklyAlKahID, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
guard error == nil else {
print(error ?? "Error with donate")
return
}
}
If you have already made such an application, then you know what a trigger is.
Use the UNCalendarNotificationTrigger trigger for your notification, in which you specify only the hour and minute (or seconds) when the notification will fire. But don't specify the day, month, etc.
For example, a notification with such a trigger will fire at 8:30
var date = DateComponents()
date.hour = 8
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question