Reputation: 6394
When scheduling a User Notification on iOS, is there a way to add an initial delay to the UNCalendarNotificationTrigger
when performing the date matching?
For example: in 3 days from now, begin sending the local notification once a day.
The reason why I want to achieve this is that my notification scheme uses different notification bodies - the first couple of days are scheduled as non-repeating, with different copy each time, and after n days I want a repeating notification to kick in.
Upvotes: 0
Views: 686
Reputation: 2809
As per the many discussions on the subject on SO, this is not possible.
You can create a certain number of local notifications for specific dates in a for loop, the limit is 50 per application I believe - and then when the app opens in the foreground AFTER 3 days you will have a chance to remove them and use the repeating notifications.
Its not the most elegant solution but if you MUST do this, then it's the only way and it should be enough, if the user doesn't open the app within the 50 days or so of queued notifications they probably won't open it again anyway.
The initial manual setup for the 3 days delayed offset would be like below:
for dayOffset in 3...33 {
let nextTriggerDate = Calendar.current.date(byAdding: .day, value: dayOffset, to: Date())!
let comps = Calendar.current.dateComponents([.year, .month, .day], from: nextTriggerDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: comps, repeats: false)
// Create a notification here
// ...
}
It's up to you to create the logic to detect when the app is opened and it's after 3 days and repeating indefinitely.
In general you could just use this permanently as you might want to differ the notification if the user hasn't responded in a while (take DuoLingo for example after several days it goes "These notifications aren't working... we'll turn them off for a while")
Upvotes: 3