iOS Notification Trigger: Fortnightly And/Or Quarterly

Trigger UILocalNotification for every 14 days (fortnightly) Swift

What you want to achieve is not possible using simple UNNotificationTriggers. For a notification to be repeated biweekly, you would need to set up a UNTimeIntervalNotificationTrigger with a timeInterval equivalent to two weeks. However, you cannot specify the first fire date of a time interval trigger, it start "ticking" as soon as you schedule it.

On the other hand, UNCalendarNotificationTrigger can be scheduled to fire at a certain date, but the problem with this is that you cannot specify a custom repeat interval for the notification trigger.

What you would need is to first, set up a non-repeating UNCalendarNotificationTrigger for the date specified by the user and once that notification is delivered, set up a UNTimeIntervalNotificationTrigger that fires every two weeks. The only issue with this approach is that the user would see a notification at the specified date as well, not only every two weeks after that. However, you can circumvent this issue by setting the notification to be delivered two weeks after the specified date, then the user won't notice the difference between the notifications.

iOS 10: repeating local user notification from specific date every month

Instead of providing a full date when you initialize the trigger, pass a set of DateComponents containing only the day of the month, and set repeats to true.

let components = DateComponents(day: 25)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

Repeating local notifications for specific days of week (Swift 3 IOS 10)

You can use below function to get Date from selected picker value:

    //Create Date from picker selected value.
func createDate(weekday: Int, hour: Int, minute: Int, year: Int)->Date{

var components = DateComponents()
components.hour = hour
components.minute = minute
components.year = year
components.weekday = weekday // sunday = 1 ... saturday = 7
components.weekdayOrdinal = 10
components.timeZone = .current

let calendar = Calendar(identifier: .gregorian)
return calendar.date(from: components)!
}

//Schedule Notification with weekly bases.
func scheduleNotification(at date: Date, body: String, titles:String) {

let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,], from: date)

let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)

let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "todoList"

let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self
//UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}

After getting a value from picker pass picker hour, minute and year with selected week day as (Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, thursday = 5, Friday = 6, Saturday = 7) to function func createDate(weekday: Int, hour: Int, minute: Int, year: Int) to get notification fire date on weekly bases, and after getting date call function func scheduleNotification(at date: Date, body: String, titles:String) for schedule a notification.



Related Topics



Leave a reply



Submit