2017-10-08 48 views

回答

0

我假設你希望用戶選擇時發送的通知,所以:

首先,授權在應用程序委託的通知,像這樣:

import UserNotifications 

class AppDelegate { 
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     let center = UNUserNotificationCenter.current() 
     center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in 
      // Enable or disable features based on authorization 
     } 
     return true 
    } 
} 

接下來,假設你」已經創建一個datepicker並將其連接到所述代碼:

@IBOutlet var datePicker: UIDatePicker! 

你的用於調度通知功能是:

func scheduleNotification() { 
    let content = UNMutableNotificationContent() //The notification's content 
    content.title = "Hi there" 
    content.sound = UNNotificationSound.default() 

    let dateComponent = datePicker.calendar.dateComponents([.day, .hour, .minute], from: datePicker.date) 
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: false) 

    let notificationReq = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger) 

    UNUserNotificationCenter.current().add(notificationReq, withCompletionHandler: nil) 
} 

你可以閱讀更多關於UserNotifications和通知觸發器在這裏:https://developer.apple.com/documentation/usernotifications

+0

謝謝你,所以我只是,只是有我將不得不改變。天,·小時的日期和月份一個選擇器。分鐘到.day。月? –

+0

是的,你應該。如果我的回答是正確的,請將其標記爲正確答案。 –

+0

好的,謝謝,我只是很快嘗試 –