1

I am working on a medicine reminder app. The user will input two dates and I need to schedule local notifications between those two dates. I can't find how to specify the starting and the ending date in the UNNotificationRequest.

I have tried this:

    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)
            }

        }
}

I am new to Swift and I am really stuck.

  • Do you mean, start sending notifications on the first date, and stop sending notifications on the second date? – SaganRitual Mar 17 '18 at 13:20
  • Yes. That is what I nee need – Anuroop Kanayil Mar 17 '18 at 13:28
  • I don't think there's an option for that. But I am not very familiar with iOS notifications. If you don't get an answer from anyone, I suggest you simply have your app check the time, and when the second time comes, turn off the notifications. – SaganRitual Mar 17 '18 at 13:30

0 Answers0