2017-01-23 17 views
0

Я хочу создать userNotification, которое будет отображаться в Notification Center во время работы приложения. Щелчок по уведомлению прекратит действие приложения.Быстрое создание уведомления пользователя

Какой триггер необходимо использовать? Как достичь этой функциональности?

ответ

0

Попробуйте это.

func scheduleNotification(at date: Date, body: String) { 
    let calendar = Calendar(identifier: .gregorian) 
    let components = calendar.dateComponents(in: .current, from: date) 
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute) 

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false) 

    let content = UNMutableNotificationContent() 
    content.title = "Dont Forget" 
    content.body = body 
    content.sound = UNNotificationSound.default() 

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger) 

    UNUserNotificationCenter.current().delegate = self 
    //UNUserNotificationCenter.current().removeAllPendingNotificationRequests() 
    UNUserNotificationCenter.current().add(request) {(error) in 
     if let error = error { 
     print("Uh oh! We had an error: \(error)") 
     } 
    } 
    }