UserNotification after 3 days, then repeated every day / hour - iOS 10

UILocalNotification has been discounted, so I would like to update my code to the scope of UserNotification:

let alertDays = 3.0 let alertSeconds = alertDays * 24.0 * 60.0 * 60.0 let localNotification:UILocalNotification = UILocalNotification() localNotification.alertAction = "Reminder" localNotification.alertTitle = "Reminder Title" localNotification.alertBody = "Reminder Message" localNotification.fireDate = Foundation.Date(timeIntervalSinceNow: alertSeconds) localNotification.repeatInterval = .day UIApplication.shared().scheduleLocalNotification(localNotification) 

How can I set a similar daily or hourly repetition using the UserNotification structure after waiting for an initial notification?

 let alertDays = 3.0 let alertSeconds = alertDays * 24.0 * 60.0 * 60.0 let content: UNMutableNotificationContent = UNMutableNotificationContent() content.title = "Reminder Title" content.subtitle = "Reminder Subtitle" content.body = "Reminder Message" let calendar = Calendar.current let alarmTime = Foundation.Date(timeIntervalSinceNow: alertSeconds) let alarmTimeComponents = calendar.components([.day, .hour, .minute], from: alarmTime) let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponents, repeats: true) let request = UNNotificationRequest(identifier: workoutAlarmIdentifier, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { (error) in // ... } 
+6
date ios10 swift repeat notifications
source share
2 answers

This seems to be unsupported, but you can use a workaround:

 let alertDays = 3.0 let daySeconds = 86400 let alertSeconds = alertDays * daySeconds let content: UNMutableNotificationContent = UNMutableNotificationContent() content.title = "Reminder Title" content.subtitle = "Reminder Subtitle" content.body = "Reminder Message" let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (alertSeconds), repeats: false) let request = UNNotificationRequest(identifier: workoutAlarmIdentifier, content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { (error) in // ... } 

in combination with didReceive (_: withContentHandler :) you can use:

 let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (daySeconds), repeats: false) 

I know this is not optimal, but it should work without using obsolete classes / methods. You use the reruns: false, because you intercept the notification immediately before the user receives it, and create a new notification. In addition, you can use it in combination with UNNotificationAction and UNNotificationCategory if you are handling multiple notifications.

+2
source share

This should work:

 func addNotificationForAlarm(alarm: MyAlarm) { let myAlarmNotifContent = UNMutableNotificationContent() myAlarmNotifContent.title = "Reminder" myAlarmNotifContent.body = alarm.activity myAlarmNotifContent.sound = UNNotificationSound.default() if alarm.repeatDays.count == 1 { } else { for index in 1...alarm.repeatDays.count { createNotif(date: alarm.date, weekDay: index, content: myAlarmNotifContent) } } } private func createNotif(date: Date, weekDay: Int, content: UNNotificationContent) { var dateComponent = DateComponents() dateComponent.weekday = weekDay dateComponent.hour = Calendar.current.dateComponents([.hour], from: date).hashValue dateComponent.minute = Calendar.current.dateComponents([.minute], from: date).hashValue let myAlarmTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true) setupNotificationSettings() let identifier = "Your-Notification" let request = UNNotificationRequest(identifier: identifier, content: content, trigger: myAlarmTrigger) let center = UNUserNotificationCenter.current() center.add(request, withCompletionHandler: { (error) in if error != nil { //TODO: Handle the error } }) } 
0
source share

All Articles