natsu1211 / Alarm-ios-swift

clone of the official IOS clock alarm app written in swift
MIT License
388 stars 116 forks source link

Having trouble to implement the new UserNotification framework. #18

Open vinnytwice opened 5 years ago

vinnytwice commented 5 years ago

Hi, I'm trying to update to new UserNotification framework, but I'm still learning it and I'm kinda lost with this old one. Is anyone updating it the new framework?

this is ho do set the notification in new framework, but I get an error, I commented it in the code:

func setRouteNotification(_ date: Date, onWeekdaysForNotify weekdays:[Int], snoozeEnabled:Bool,  onSnooze: Bool, soundName: String, routeName: String, index: Int) {
        // Notification content
        let routeCheckNotificationContent = UNMutableNotificationContent()
        let datesForNotification = correctDate(date, onWeekdaysForNotify: weekdays)
        routeCheckNotificationContent.title = "Hello!! Are you ready to cycle?"
        routeCheckNotificationContent.body = "Check route for alerts?"
        routeCheckNotificationContent.categoryIdentifier = Id.checkActionIdentifier
        routeCheckNotificationContent.sound = UNNotificationSound.init(named: soundName + ".mp3") // check for the + ".mp3"

        // Define actions
        let check = UNNotificationAction(identifier: Id.checkActionIdentifier, title: " Check", options: [])
        let wait = UNNotificationAction(identifier: Id.waitActionIdentifier, title: "Wait", options: [])
        // Define category
        let routeCategory = UNNotificationCategory(identifier: Id.notificationCategory, actions: [check, wait], intentIdentifiers: [], options: [])
        // Register category
        UNUserNotificationCenter.current().setNotificationCategories([routeCategory])

        //part of old code : errors to correct, do we need it?
        let actionsArray = snoozeEnabled ? [UNNotificationAction](arrayLiteral: wait, check) : [UNNotificationAction](arrayLiteral: wait)
        let actionsArrayMinimal = snoozeEnabled ? [UNNotificationAction](arrayLiteral: wait, check) : [UNNotificationAction](arrayLiteral: check)
        // Specify the category related to the above actions.
        //        let alarmCategory = UNNotificationCategory()
        //        alarmCategory.identifier = "myAlarmCategory"
        //        routeCategory.setActions(actionsArray, for: .default)
        //        alarmCategory.setActions(actionsArrayMinimal, for: .minimal)
        let repeating: Bool = !weekdays.isEmpty
        routeCheckNotificationContent.userInfo = ["snooze" : snoozeEnabled, "index": index, "soundName": soundName, "routeName": routeName, "repeating" : repeating]
        //repeat weekly if repeat weekdays are selected
        //no repeat with snooze notification
        if !weekdays.isEmpty && !onSnooze{
        }

        //        let datesForNotification = correctDate(date, onWeekdaysForNotify:weekdays)

        syncAlarmModel()
        for d in datesForNotification {
            if onSnooze {
                alarmModel.alarms[index].date = Scheduler.correctSecondComponent(date: alarmModel.alarms[index].date)
            }
            else {
                alarmModel.alarms[index].date = d
            }
            // Notification trigger
            let pickedDate = d
            let calendar = Calendar(identifier: .gregorian)

            calendar.repeatInterval = NSCalendar.Unit.weekOfYear // Value of type 'Calendar' has no member 'repeatInterval' ERROR

            let components = calendar.dateComponents(in: .current, from: d)
            let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute, second: components.second)
            let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
            // Notification Request

            let routeNotificationRequest = UNNotificationRequest(identifier: "routeNotificationRequest", content: routeCheckNotificationContent, trigger: trigger)

            // Add request

            UNUserNotificationCenter.current().add(routeNotificationRequest) { (Error) in
                if let error = Error {
                    print("sukaaaa")
                }

            }

        }

    }

Also I see that fireDate is depreciated so I don't know how to adapt check Notification()

func checkNotification() {
    alarmModel = Alarms()
    let notifications = UIApplication.shared.scheduledLocalNotifications
    if notifications!.isEmpty {
        for i in 0..<alarmModel.count {
            alarmModel.alarms[i].enabled = false
        }
    }
    else {
        for (i, alarm) in alarmModel.alarms.enumerated() {
            var isOutDated = true
            if alarm.onSnooze {
                isOutDated = false
            }
            for n in notifications! {
                if alarm.date >= n.fireDate! {
                    isOutDated = false
                }
            }
            if isOutDated {
                alarmModel.alarms[i].enabled = false
            }
        }
    }
}
mobileonekh commented 5 years ago

Look at the scale of of the code, updating to the new UINotification framework is not easy