iOS Notification Trigger: fortnightly and/or quarterly - ios

I can't seem to find any Apple Documentation for this exact scenario, and I've tried various ways to do this and I keep coming up empty.
I would like to schedule a repeating notification (iOS 10+ so UNCalendarNotificationTrigger or equivalent).
These are Local Notifications not Push Notifications.
My Goal:
Schedule notifications that repeat:
once a fortnight (e.g., every second Tuesday)
once a quarter (e.g., 1st of every 3 months)
My Current Approach:
These triggers work well, and are simple to implement (Running the code in a Swift Playground).
// 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 can't seem to get 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:
Is it possible to get a notification that repeats every fortnight?
How do we get a trigger for once a quarter?
Since DateComponents() has a quarter unit, I assume that a quarterly trigger is possible. For the fortnightly reminder however, I'm not even sure if this is possible...
Any insight would be appreciated!

I didn't see any direct option to trigger fortnight notification.Suggestion from my end.
1) Is it possible to get a notification that repeats every fortnight?
I am proposing two options:
Can we use UNTimeIntervalNotificationTrigger to repeat the notification with a time interval of two weeks time? 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 trigger, which should trigger first and third day of a month. For example it should fire notification in first Sunday and third Sunday of a 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 do we get a trigger for once a quarter?
If there is no direct option available, then I suggest the same solution as above.

Related

UNCalendarNotificationTrigger with start date

I am trying to make a reminder app and I am having a hard time trying to set a start date for my reminders. I am able to make local Notification work with the code below. With this code, I am able to get local notification every Tuesday at the time set by the user (Indicated by the "hour" and "minute").
if tuesdayIsChecked == true {
AlarmSupportFileX.tuesdaySelectedBool = true
hour = hourDateFormatter.string(from: datePicker.date)
minute = minuteDateFormatter.string(from: datePicker.date)
var dateComponents = DateComponents()
dateComponents.weekday = 3
dateComponents.hour = Int(hour)
dateComponents.minute = Int(minute)
print(dateComponents)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let requestIdentifier = "\(randomGeneratedString)Tuesday"
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
//Handle error
})
}
My issue is that I only want it to trigger on the Date chosen by user.
Example: If I use my code above on 12th Nov 2018 (Monday), a notification will trigger on 13th Nov 2018 (Tuesday) and all the Tuesday that follows.
Assuming user select alarm to trigger on tuesday (dateComponents.weekday = 3).If user set a notification on 12th Nov(Monday) and they selected a start date on 25th Nov(Sunday), I want it to only show the notification on 27th Nov(Tuesday) and all the Tuesday that follows. How can I achieve this?
This is not possible using only UNCalendarNotificationTrigger.
You would need to set up some sort of mechanism for the app to set up that notification the week of the Tuesday.
One way to do this could be:
check if the selected date is this week
-> IF TRUE: register notification
-> IF FALSE: write to UserDefaults the date.
In an didBecomeActive() or viewDidLoad() you could then load the date from UserDefaults and check if the date is in this week, if that's the case, you register the notification.

Set repeat notification trigger using DateComponents

Currently working on an App need set different type of repeat rule of notification
Like repeat everyday, workday, weekday or several days of the week.
var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
dateInfo.weekday = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
Using this API can only add one day of the week at each time,
is there any simple way.
appreciated

Notification with weekly repeat, and specific start date

I can set a UNNotification to fire at a specific date/time, like so
let components = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: fireTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
And I can set one to fire at a specific time on a specific day of the week, every week, like so
var components = Calendar.current.dateComponents([.weekday], from: nextMondayDate)
components.setValue(10, for: .hour)
let trigger = UNCalendarNotificationTrigger.init(dateMatching: triggerComps, repeats: true)
But is there any way I can set a UNNotification to fire at a specific date and time, and then to repeat every week on that weekday and time?
Specifically, I would like to set a notification to fire every Monday at 10am except next Monday.
if you just want to skip only next monday, you can calculate next to next monday date and set in the components on the place of nextMondayDate keeping rest of the setting as it is, like repeat true.

Using a UNCalendarNotificationTrigger with dateMatching produces a trigger date 5 hours after the intended time

I'm trying to schedule an iOS notification at the same hour and minute every day.
First I define a DateComponents:
var components = DateComponents()
components.hour = 1
components.minute = 10
components.second = 0
Then use this to make the trigger:
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
Printing the components yields the expected result:
hour: 1 minute: 10 second: 0 isLeapMonth: false
But printing the trigger's next trigger date always shows a time 5 hours later than what it should:
Optional(2017-07-18 06:10:00 +0000)
Any idea what's going on here? I've tried setting the time zone on the components and it doesn't have any effect.
Edit: It looks like the notifications are actually firing at the correct time (I had a different issue which had broken them). I still can't explain the behavior of nextFireDate though.

Schedule local notification at specific day and hour

How can I schedule a local notification that fire every Wednesday and Saturday at noon local device time?
let localNoonSatNotification:UILocalNotification = UILocalNotification()
localNoonSatNotification.userInfo = ["uid":"noonBreak"]
localNoonSatNotification.alertAction = "Noon Break"
localNoonSatNotification.alertBody = "Time for a break! Come and play few levels"
localNoonSatNotification.fireDate = // get next Wednesday/Saturday 12:00 PM
localNoonSatNotification.soundName = UILocalNotificationDefaultSoundName
localNoonSatNotification.applicationIconBadgeNumber = 1
UIApplication.sharedApplication().scheduleLocalNotification(localNoonSatNotification)
If someone still needs help with this question UNCalendarNotificationTrigger
// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.weekday = 3 // Tuesday
dateComponents.hour = 14 // 14:00 hours
// Create the trigger as a repeating event.
let trigger = UNCalendarNotificationTrigger(
dateMatching: dateComponents, repeats: true)
You can set repeat interval,
notification.repeatInterval = NSCalendarUnit.CalendarUnitWeekday
Hope this will help :)

Resources