R
R
Rodion Bizyaev2020-02-12 09:06:35
Swift
Rodion Bizyaev, 2020-02-12 09:06:35

How to make local notifications in Swift?

Good day!
I figured out how to send pushes using a pusher.
But is it possible to sew pushes into the code itself, so that they would be sent without my knowledge (once an hour for example)? It's just that the point is that pushes with certain phrases would come throughout the day.
I want to install the application only on my devices through test on device.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2020-02-12
@ivanvorobei

Can. Here is the code:

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.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question