1

我正在开发一个药物提醒应用程序。用户将输入两个日期,我需要在这两个日期之间安排本地通知。我找不到如何在UNNotificationRequest.

我试过这个:

    static func setAlarmWithDate (hour:Int, minutes:Int , day:Int, medicineName:String, medicineId:String) {

        let content = UNMutableNotificationContent()
        content.title = NSString.localizedUserNotificationString(forKey: "MY_MEDICINE".localizedString(), arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "This is a notification for your medicine \(medicineName)",
            arguments: nil)
        content.sound = UNNotificationSound.default()
        var userInfo = [String:String]()
        let medicine = medicineId
        userInfo["medicine"] = medicine
        content.userInfo = userInfo
        var dateInfo = DateComponents()
        dateInfo.hour = hour
        dateInfo.minute = minutes
        dateInfo.weekday = day


        let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)

        let identifier = medicineId


        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)


        let center = UNUserNotificationCenter.current()

        center.add(request) { (error : Error?) in
            if let theError = error {
                print(theError.localizedDescription)
            }

        }
}

我是 Swift 的新手,我真的被困住了。

4

0 回答 0