2017-10-08 55 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

好的,谢谢,我只是很快尝试 –

相关问题