我有这个数组它已经填充在带有动态单元格的表视图中,我想要它创建一个本地议程或通知或警报或类似的东西,以提醒用户在显示的当天和小时服用药物在阵列中,你能帮帮我吗?请。非常感谢。
{“medicine”:”paracetamol”
“day”:”tuesday”
“time”:”9:00”},
{“medicine”:”aspirine”
“day”:”friday”
“time”:”16:00”},
{“medicine”:”pills”
“day”:”monday”
“time”:”22:00”}
答案 0 :(得分:0)
您必须创建一个日历对象,并在comps.weekday,comps.hour和comps.minute属性中设置工作日和时间值。
对于您的消息,请在通知content.body属性中设置。
import UserNotifications
let calendar = NSCalendar.init(calendarIdentifier: .gregorian)
calendar?.locale = NSLocale.current
let year = calendar?.component(.year, from: date)
let month = calendar?.component(.month, from: date)
let cal = Calendar.current
var comps = cal.dateComponents([.weekOfYear, .yearForWeekOfYear], from: date)
comps.weekday = i // Monday - Friday
comps.hour = j
comps.minute = minute
comps.year = year
comps.month = month
let notificationDate = cal.date(from: comps)!
let interval = String.init(format: "%d%d%d", i,j,minute)
if #available(iOS 10.0, *) {
let triggerDate = Calendar.current.dateComponents([.weekday,.hour,.minute], from: notificationDate as Date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate,
repeats: true)
let content = UNMutableNotificationContent()
content.title = DataManager.sharedInstance.notificationTitle
content.body = String.init(format: "%@", DataManager.sharedInstance.notificationMessage)
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: String(interval), content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print(error)
}
}
} else {
// ios 9
// Fallback on earlier versions
let notification = UILocalNotification()
/* Time and timezone settings */
notification.fireDate = date
notification.repeatInterval = NSCalendar.Unit.weekday
notification.timeZone = NSCalendar.current.timeZone
notification.alertBody = "message"
notification.userInfo = [ "title" : String(interval)]
/* Schedule the notification */
UIApplication.shared.scheduleLocalNotification(notification)
}
更新的答案