2016-09-07 70 views
0

因此,我一直在试图实现iOS 10本地通知,首先我认为代码与iOS 9相同,只是添加通知扩展目标。然而,它实际上使用UNMutableNotificationContent类,本地UNMutableNotificationContent使用日期选择器的启动日期

我可以使用时间间隔设置通知与通知,但是我怎样才能使用日期选择器专门设置启动日期?

import UserNotifications 

let dateFormatter = DateFormatter() 
dateFormatter.dateStyle = .full 
let displayDate = dateFormatter.string(from: datePicker.date) 

let notif = UNMutableNotificationContent() 

notif.title = "Reminder)" 
notif.subtitle = "\(displayDate)" 
notif.body = "Details" 
notif.categoryIdentifier = "Details Reminder" 

let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 

let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: notifTrigger) 

UNNotificationRequest触发需要的类型UNTimeIntervalNotificationTrigger,我希望我可以只添加一个日期。

我必须使用当前日期和日期选择器日期来计算间隔时间吗?或者我可以用其他方法做到这一点

回答

2

您需要提供'now'和希望发送通知的时间间隔。

let interval = laterDate.timeIntervalSince(earlierDate) 

在这种情况下,earlierDate应该是 '现在'(即只Date()),我认为laterDate是你datePicker.date

2

您应该使用UNCalendarNotificationTrigger。

UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: <<YOUR_DATE>>), repeats: false) 
相关问题