Set repeat notification trigger using DateComponents - ios

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

Related

Repeat local notification every n days

I'm trying to create application that will send local notifications every n days.
I has DailyRepeat structure that contains notification info:
struct DailyRepeat: BaseRepeat {
var title: String
var body: String
var date: Date
var day: Int
}
And method that schedule notification:
func notifyDaily(at notification: DailyRepeat) {
let content = generateContent(title: notification.title, body: notification.body)
let dateComponents = DateComponents(day: notification.day, hour: notification.date.time.hours, minute: notification.date.time.minutes)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
notificationCenter.add(request)
}
My first thought was to create UNCalendarNotificationTrigger on first fire date, than handle notification and set UNTimeIntervalNotificationTrigger, but unfortunately I cannot find the way to handle notification receive without user interaction.
Any thoughts how it should work?
As per the documentation if you want the notification to repeat you need to set repeatable constraints for the date components.
So to set a notification to run every morning at 8:30AM you would just set the hour and the minute and set it to repeat.
var date = DateComponents()
date.hour = 8
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
I don't think that you can use repeats to repeat every x number of days, you can set specific days of the week, or dates of the month, but not every n days.
You could set a TimeInterval of x number of days and repeat that, but getting the exact time to start it might be tricky.

How to repeat local notification once fired at a particular time

I am currently making an alarm clock app and I am currently stuck at possibly just a misunderstanding. I want to launch a local notification at a specific time and repeat at a time interval. I have been getting a couple ideas and this (https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/) program is close to what I want it to be but I want it to go off at a certain time and then repeat at a single interval.
After user inputs information, the program will spit out:
timeIntervalSeconds, the interval that user wants the notifications in (in seconds)
timeStart, when the notifications will start
timeEnd, when the notifications will end if the user doesn't stop them
Any suggestions would be greatly appreciated.
Thank you!
To launch notification on specific time use below code.
let gregorian = Calendar(identifier: .gregorian)
let now = Date()
var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
// Change the time to 10:00:00 in your locale
components.hour = 10
components.minute = 0
components.second = 0
let date = gregorian.date(from: components)!
let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: CommonViewController.Identifier, content: content, trigger: trigger)
And to repeat notifications on specific time interval use below code in trigger.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true) // it repeats after every 2 minutes
Since Apple provide only limited list of intervals for auto repeat of notifications you can try to do next to use custom intervals:
When a notification got fired you should calculate date & time for next notification and just schedule it on desired time.

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.

iOS Notification Trigger: fortnightly and/or quarterly

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.

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