sguruapp@gmail.com
facebook vk instagram Телеграм youtube

Пример работы с локальными уведомлениями (Local Notification) - Swift 5


// Как пример, создадим функцию, которая вернёт дату и время для работы с уведомлением
func createDate(day: Int, month : Int, hour: Int, minute: Int, year: Int)->Date {

var components = DateComponents()
components.hour = hour
components.minute = minute
components.year = year
components.day = day
components.month = month

components.timeZone = .current

let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: components)!
}



// Создаём уведомление
func scheduleNotification(at date: Date, identifierUnic : String, body: String, titles:String) {

let triggerWeekly = Calendar.current.dateComponents([.day, .month, .hour,.minute, .year], from: date)

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)

let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default
content.categoryIdentifier = "todo"

let request = UNNotificationRequest(identifier: identifierUnic, content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print(" We had an error: \(error)")
}
}

}


Использование

scheduleNotification(at: createDate(day : 11, month : 2, hour: 15, minute: 5, year: 2018), identifierUnic: "unic1", body: "Notification day", titles: "Notification titles")

Возврат к списку

обучись профессии будущего

Хочешь стать успешным разработчиком
приложений? Тогда добро пожаловать к нам.

обучись профессии будущего