Local Notification in swift 3 repeat interval selected by users

790 views Asked by At

I'm trying to write some code which will allows users to set custom local notifications to remind them some tasks. I'm using the user notification framework. So basically a user can decide to set a notification which will remind him to buy some soap every week or to make birthday wishes every year or to check something every month (these are just examples).

So far I've been able to set a local notification with no repeating interval.

func scheduleNotif() {
    let dateformatter = DateFormatter()
    dateformatter.dateStyle = DateFormatter.Style.medium
    dateformatter.timeStyle = DateFormatter.Style.short
    let dateFromString = dateformatter.date(from: selectDateTextField.text!)
    let fireDateOfNotification: Date = dateFromString!

    //if notif are allowed
    let content = UNMutableNotificationContent()
    content.title = notifTitleTextField.text!
    content.body = notifNoteTextView.text
    content.sound = UNNotificationSound.default()
    content.badge = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber

    let triggerDate = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: fireDateOfNotification)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate,
                                                repeats: false)
    //Schedule the Notification
    let titleNospace = notifTitleTextField.text?.replacingOccurrences(of: " ", with: "")
    let identifier = titleNospace
    let request = UNNotificationRequest(identifier: identifier!, content: content, trigger: trigger)
    self.center.add(request, withCompletionHandler: { (error) in
        if let error = error {
            print(error.localizedDescription)
        }
    })
}

The date is picked from a date picker. Now from another picker i would like the user to select the repeating interval (none, daily, weekly, monthly, yearly and on specific days). I'm not sure how I can achieve this. Do I need to use an if else if statement? (it doesn't seems really correct to me.) Is there a better way to achieve this? Thank you!

1

There are 1 answers

0
Marco On

For the moment I've fixed it this way:

let dateformatter = DateFormatter()
dateformatter.dateStyle = .medium
dateformatter.timeStyle = .short
let dateFromString = dateformatter.date(from: selectDateTextField.text!)
let fireDateOfNotification: Date = dateFromString!

var trigger: UNCalendarNotificationTrigger
var triggerDate: DateComponents
var repeatInterval = Bool()

if repeatingInterval == "Hourly" {
    triggerDate = Calendar.current.dateComponents([.minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Daily" {
    triggerDate = Calendar.current.dateComponents([.hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Weekly" {
    triggerDate = Calendar.current.dateComponents([.weekday, .hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Monthly" {
    triggerDate = Calendar.current.dateComponents([.day, .hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else if repeatingInterval == "Yearly" {
    triggerDate = Calendar.current.dateComponents([.month, .day, .hour, .minute], from: fireDateOfNotification)
    repeatInterval = true
} else {
    triggerDate = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: fireDateOfNotification)
    repeatInterval = false
}

trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: repeatInterval)

//Schedule the Notification
......

Haven't find yet a way to schedule it for different interval (e.g. every two weeks, every second day, every six months etc). I will still look into it. :)