IOS 10 - Repeat notifications every minute "x"

In iOS 10, how can I configure local notifications to be repeated in minutes, starting from a specific date / time.

For example, initiate a local notification every 15 minutes, starting at 11:00 on September 8? Suppose below DateTimeReminder.date is 09/08 11 a.m.

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 code above, I have the opportunity to schedule at a specific minute of every hour, at a specific hour of every day, and so on. But how do I turn it into "every" x "minutes"? Any help is appreciated.

A similar question is - How to set NSCalendarUnitMinute repeatInterval in UserNotifications iOS 10?

+4
source share
6

, , Swift 3 . , , UILocalNotification ( iOS 10), UNUserNotification . , . , , , , .

  • , API. , API. .

http://useyourloaf.com/blog/local-notifications-with-ios-10/

https://github.com/lionheart/openradar-mirror/issues/14941

+2

, 64 . , 64 .

, , - 64 , ( ) 64 , n , (64 - n) .

int n = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
int x = 64 - n;
// Schedule the next 'x' notifications

NSTimer X .

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
}

, .

+3

, , , . , ,

var date = DateComponents()
date.hour = 8
date.minute = 30

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let content = UNNotificationContent()
// edit your content

let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)
+1

Swift 3/4 iOS 10/11:

, DateComponents() .

TimeInterval ( , 60 ):

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)
+1

, , .

- x Swift

"60.0" . x * 60

0

You can first use the UNCalendarNotificationTrigger to trigger a notification on a specific date and set the repeat property to false so that it notifies only once. When you receive this notification, use the UNTimeIntervalNotificationTrigger API to set the time interval at which you want to call the local notification

0
source

Source: https://habr.com/ru/post/1654013/


All Articles