Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 10 - Repeating notifications every "x" minutes

Tags:

ios

ios10

swift

In iOS 10, how can I set local notifications to repeat in minutes starting from a particular date/time.

For example, trigger local notification every 15 minutes starting from 11 AM on 8th September? Assume that, below, dateTimeReminder.date has 09/08 11 AM.

let dateStart = self.dateTimeNotif.date
let notifTrigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: dateStart), repeats: true)
let notificationRequest = UNNotificationRequest(identifier: "MYNOTIF", content: notifContent, trigger: notifTrigger)

UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: nil)

With the above code, I have a possibility to schedule at a particular minute of every hour, at a particular hour of each day and so on. But how do I turn it into "every "x" minutes"? Any help is appreciated.

Similar question - How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?

like image 290
DS. Avatar asked Sep 08 '16 12:09

DS.


1 Answers

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)
like image 159
Alessandro Ornano Avatar answered Nov 06 '22 23:11

Alessandro Ornano