IOS notification trigger: bi-weekly and / or quarterly

I cannot find any Apple documentation for this exact scenario, and I tried various ways to do this, and I keep going empty.

I would like to schedule a re-notification ( iOS 10+ so UNCalendarNotificationTrigger or equivalent).
These are local alerts, not push notifications.

My goal:

Schedule of notifications repeating:

  • once every two weeks (for example, every second Tuesday)
  • once a quarter (e.g. 1 out of every 3 months )

My current approach:

These triggers work well and are easy to implement (running Swift Playground code).

 // Every day at 12pm var daily = DateComponents() daily.hour = 12 let dailyTrigger = UNCalendarNotificationTrigger(dateMatching: daily, repeats: true) dailyTrigger.nextTriggerDate() // "Jan 4, 2017, 12:00 PM" // Every Tuesday at 12pm var weekly = DateComponents() weekly.hour = 12 weekly.weekday = 3 let weeklyTrigger = UNCalendarNotificationTrigger(dateMatching: weekly, repeats: true) weeklyTrigger.nextTriggerDate() // "Jan 10, 2017, 12:00 PM" // The 1st of every month at 12pm var monthly = DateComponents() monthly.hour = 12 monthly.day = 1 let monthlyTrigger = UNCalendarNotificationTrigger(dateMatching: monthly, repeats: true) monthlyTrigger.nextTriggerDate() // "Feb 1, 2017, 12:00 PM" // Every 1st of February at 12pm var yearly = DateComponents() yearly.hour = 12 yearly.day = 1 yearly.month = 2 let yearlyTrigger = UNCalendarNotificationTrigger(dateMatching: yearly, repeats: true) yearlyTrigger.nextTriggerDate() // "Feb 1, 2017, 12:00 PM" 

But...

I cannot force a fortnightly or quarterly trigger to function correctly.

 // Every second Tuesday at 12pm // ... There is no "date.fortnight", is this possible? // The 1st of every quarter at 12pm var quarterly = DateComponents() quarterly.hour = 12 quarterly.day = 4 // Values: 1, 2, 3 or 4 all produce the same "nextTriggerDate" - why? quarterly.quarter = 4 let quarterlyTrigger = UNCalendarNotificationTrigger(dateMatching: quarterly, repeats: true) quarterlyTrigger.nextTriggerDate() 

So, to be clear, my questions are:

  • Can I get a notification that repeats every two weeks?
  • How do we get a trigger for a quarter?

Since DateComponents() has a quarter block, I assume a quarterly trigger is possible. However, for a one-week reminder, I'm not even sure that this is possible ...

Any insight would be appreciated!

+6
source share
1 answer

I did not see any direct option to trigger a notification in two weeks. Difficulty from my end.

1) Can I get a notification that repeats every two weeks?

I offer two options:

  • Can I use UNTimeIntervalNotificationTrigger to repeat a notification with a two-week interval? Example below

     let timeInterValFor2Weeks = 1190507.790425003 let intervalTrigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterValFor2Weeks, repeats: true)//"Jan 3, 2017, 5:24 PM" intervalTrigger.nextTriggerDate() //"Jan 17, 2017, 12:05 PM" 
  • Schedule two UNCalendarNotificationTrigger triggers that should trigger the first and third day of the month. For example, he must notify on the first Sunday and third Sunday of the month.

     var fortnightPart1 = DateComponents() fortnightPart1.weekday = 1 //(Day..here Sunday) fortnightPart1.weekdayOrdinal = 2 //= n == (nth Day in the month...here 2nd Sunday in every month month) fortnightPart1.hour = 12 let fortnightTrigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: true) fortnightPart1.nextTriggerDate() 

2) How to get a trigger for a quarter?

If there is no direct access, then I propose the same solution as above.

+3
source

Source: https://habr.com/ru/post/1013720/


All Articles