R
R
Rodion Bizyaev2020-02-16 14:12:49
Notifications
Rodion Bizyaev, 2020-02-16 14:12:49

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

2 answer(s)
I
Ivan Vorobei, 2020-02-16
@ivanvorobei

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
        }
}

In this example , a notification will be delivered every 9 am . Note that trigger has repeat. Don't forget to ask for permission before pushing .
PS This is a trivial question. You are probably a beginner developer. It is not customary in the community to contact the toaster with any problem, the first 5 links in Google to the question local notification swift are also good answers.

N
NSA-bot, 2020-02-16
@NSA-bot

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 question

Ask a Question

731 491 924 answers to any question