iOS 10 - Repeating notifications every "x" minutes
Swift 3/4 and iOS 10/11:
According with this bug seems there is no way to use DateComponents()
to repeat correctly a local notification.
Instead of this method you can change your trigger with TimeInterval
(this method works if you interval is major than 60 seconds):
let thisTime:TimeInterval = 60.0 // 1 minute = 60 seconds
// Some examples:
// 5 minutes = 300.0
// 1 hour = 3600.0
// 12 hours = 43200.0
// 1 day = 86400.0
// 1 week = 604800.0
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: thisTime,
repeats: true)
As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.
One way to make sure all notifications get scheduled is to schedule the first 64 notifications first, and then on regular time intervals (may be on every launch of the app or each time a notification fires) check for the number of notifications scheduled and if there are less than 64 notifications, lets say n notifications, then schedule the next (64 - n) notifications.
int n = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
int x = 64 - n;
// Schedule the next 'x' notifications
for rest add a NSTimer for X minutes to come and set the notification.
override func viewDidLoad() {
super.viewDidLoad()
//Swift 2.2 selector syntax
var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: #selector(MyClass.update), userInfo: nil, repeats: true)
//Swift <2.2 selector syntax
var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: "update", userInfo: nil, repeats: true)
}
// must be internal or public.
func setNotification() {
// Something cool
}
Make sure you remove old and not required notifications.
After searching around quite a bit, I've come to the conclusion that Swift 3, at this point, doesn't support this feature. For everyone looking for this functionality, I'd suggest using UILocalNotification for now (although deprecated in iOS 10), but later migrate to UNUserNotification once it supports this feature. Here are some additional questions and resources that have helped me to reach this conclusion. Also, please follow all the answers and comments in this thread to get more insight into which particular scenario it talks about.
- It is usually a bad idea to use deprecated APIs. As a general practice, migrate to new APIs as soon as possible. The above solution is NOT recommended as a permanent solution.
Local Notification every 2 week
http://useyourloaf.com/blog/local-notifications-with-ios-10/
https://github.com/lionheart/openradar-mirror/issues/14941