I'm writing an app that needs to present repeating notifications on a schedule. The schedule will be every x months, where x is a number 1-72. So, it could be every 1 month, every 7 months, every 45 months, etc..
This is my first venture into user notifications, and I'm struggling with the DateComponents
piece.
I see the documentation says DateComponents.month
= a month or count of months. I interpret that to mean that the value could be an Int to represent a specific month (like 1 = January) or an Int to represent the number of months used for repetition (ex: 2 = every 2 months).
For my test, I wanted to manually create a user notification trigger that would repeat every 1 month on the first day of the month at 7:00am.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.month = 1
dateComponents.day = 1
dateComponents.hour = 7
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
That didn't work as expected. It created the notification for January 1st at 7:00am, rather than the 1st of each month. I expect it would have repeated on the next calendar year.
What am I doing wrong? What is needed to achieve my goal of repeating every x months?