2017-01-23 57 views
0

我想創建一個userNotification,它會在應用程序運行時顯示在Notification Center中。 點擊通知將終止該應用程序。swift創建用戶通知

需要使用哪個觸發器? 如何實現此功能?

回答

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