2

I'm setting up some local notifications as reminders. So far I've been able to set a non repeating notification which triggers from a date picked from a datePicker.

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 permission allowed
    let content = UNMutableNotificationContent()
    content.title = notifTitleTextField.text!
    content.body = notifNoteTextView.text
    content.sound = UNNotificationSound.default()

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

Now i would like the user to pick from a list (or a Picker) the repeating interval (hourly, daily, weekly, monthly, yearly or every x days). Is there a simple way to do so or i need to create a custom class? is it correct to make it through a series of if else statement? (it seems to me a bit off, doesn't really seems to be the correct way) Thanks.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Marco
  • 1,051
  • 1
  • 17
  • 40
  • I recommend you reading [this](https://stackoverflow.com/a/41901767/3687801) before you're trying to implement such a thing!! – nayem Aug 07 '17 at 10:26
  • @nayem thx but i don't need push notification... what i'm trying to do is simply to let user set a reminder (e.g clean the swimming pool filter) and set a repeating interval (e.g each month). Local notification are good fit for this. all the infos are set by the user – Marco Aug 07 '17 at 10:54

1 Answers1

2

If you set up a UNCalendarNotificatonTrigger, you cannot manipulate its repetition interval when you set repeats = true, since it will be repeating at each date when the date components of the trigger are matched. To give you an example, if you only set up the hour, minute and second components, your notification will be repeated daily, since the exact hour, minute and second values occur each day. If you only set up minute and second, the notification will repeat every hour.

What you are looking for is the UNTimeIntervalNotificationTrigger, this is the one using which you can set up the repetition interval.

However, to match your criteria, you need to mix both triggers. You should first set up a non-repeating UNCalendarNotificationTrigger and once that notification is delivered, set up a repeating UNTimeIntervalNotificationTrigger with the time interval coming from the user. See UNTimeIntervalNotificationTrigger

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • thank for the answer. So basically i should use this UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: {deliveredNotifications -> () in and in here set the UNTimeIntervalNotificationTrigger?but how can i the specific identifier if i don't set it once i schedule the notification but after is delivered? – Marco Aug 07 '17 at 10:49
  • Yes, if you want to access the notifications not opened by the user, use that method. You can store arbitrary data in `UNNotificationContent`'s `userInfo` property, so you can store the time interval there and then check this value in `getDeliveredNotifications` for each delivered notification. Of just store the identifier of the scheduled notifications in a dictionary along with the time intervals corresponding to each notification. – Dávid Pásztor Aug 07 '17 at 10:52
  • ok... but you said notification not opened... what if the user tapped on the notification, will not be repeated anymore? sorry but i'm totally new to this... – Marco Aug 07 '17 at 10:57
  • `getDeliveredNotifications` only returns the notifications that are still in the `Notification Center`, meaning that the user have not opened them yet. Since your first notification set up with the `UNCalendarNotificationTrigger` will not be repeating, it will be removed from `Notification Center` as soon as user opens it. For this reason, you should schedule the notification with the `UNTimeIntervalNotificationTrigger` as soon as the user receives the first notification at the specified date. – Dávid Pásztor Aug 07 '17 at 11:04
  • For more information, see the [Local and Remote notification Programming guide](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html#//apple_ref/doc/uid/TP40008194-CH5-SW1) and especially the *Responding to the Delivery of Notifications* part. – Dávid Pásztor Aug 07 '17 at 11:04
  • No worries. If you found my answer helpful, please consider accepting it. – Dávid Pásztor Aug 07 '17 at 11:06
  • sure... i'l try and then get back.. :) – Marco Aug 07 '17 at 11:12
  • I don't believe this would work as you cannot execute any code on notification delivery if the app is not active. Correct me if I am wrong... – calebmanley Oct 08 '17 at 02:25