I am trying to trigger UNUserNotification
fortnightly which will start from a specific future day. I have triggered successfully with UNTimeIntervalNotificationTrigger
. But my problem is I cannot set a specific start date here.
UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:(14*24*3600) repeats: YES];
request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
I've tried with WeekdayOrdinal
for UNCalendarNotificationTrigger
but that does not work exactly with fortnight duration always.
Is there any way I can schedule local UNUserNotification
fortnightly from a specific day in future?
UNNotificationRequest
does not provide anything to trigger fortnight recurring.
With UNCalendarNotificationTrigger
you can only trigger following types of recurring:
weekly
, daily
, monthly
, yearly
switch repeatType {
case .daily:
newComponents.hour = components.hour
newComponents.minute = components.minute
break
case .weekly:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.weekday = components.weekday
break
case .monthly:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.day = components.day
break
case .none:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.day = components.day
newComponents.month = components.month
newComponents.year = components.year
break
case .annually:
newComponents.hour = components.hour
newComponents.minute = components.minute
newComponents.day = components.day
newComponents.month = components.month
break
}
Although you can easily use EventKit
to create events and alarms within those events with all such recurring options you want like the fortnight, quarterly, half-yearly etc.
I have created this code to create events with multiple recurring types:
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = "Countdown title"
event.startDate = Date()
event.notes = "Countdown is complete!"
event.isAllDay = false
event.calendar = eventStore.defaultCalendarForNewEvents
var frequency : EKRecurrenceFrequency = EKRecurrenceFrequency.daily
var interval = 1
switch repeatType {
case .daily:
//Repeat every day
frequency = EKRecurrenceFrequency.daily
interval = 1
break
case .weekly:
//Repeat every week
frequency = EKRecurrenceFrequency.weekly
interval = 1
break
case .biweekly:
//Repeat every 2 weeks
frequency = EKRecurrenceFrequency.weekly
interval = 2
break
case .monthly:
//Repeat every month
frequency = EKRecurrenceFrequency.monthly
interval = 1
break
case .quarterly:
//Repeat every 3 months
frequency = EKRecurrenceFrequency.monthly
interval = 3
break
case .halfYearly:
//Repeat every 6 months
frequency = EKRecurrenceFrequency.monthly
interval = 6
break
default:
// Repeat every year
frequency = EKRecurrenceFrequency.yearly
interval = 1
}
let alarm : EKAlarm = EKAlarm(relativeOffset: TimeInterval(exactly: 0)!)
event.addAlarm(alarm)
if interval > 0 {
event.addRecurrenceRule(EKRecurrenceRule(recurrenceWith: frequency, interval: interval, end: nil))
}
do {
try eventStore.save(event, span: .thisEvent)
} catch let error as NSError {
print(error.localizedDescription)
return
}
} else {
print(error?.localizedDescription ?? "no error")
}
})
Please don't forget to import EventKit
.
Try with triggerWithDateMatchingComponents
method
Objective-C Example Code
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:86400]; // You can set your date here.
NSDateComponents *triggerDate = [[NSCalendar currentCalendar]
components:NSCalendarUnitYear +
NSCalendarUnitMonth + NSCalendarUnitDay +
NSCalendarUnitHour + NSCalendarUnitMinute +
NSCalendarUnitSecond fromDate:date];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:NO];
Swift Example
let triggerDate = Calendar.current.date(byAdding: .day, value: 1, to: self)!
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.