Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserNotification in 3 days then repeat every day/hour - iOS 10

UILocalNotification has been depreciated so I would like to update my code to the UserNotification framework:

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 repeat with the UserNotification framework after waiting for the 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 // ...
    }
like image 977
Greg Robertson Avatar asked Jul 14 '16 17:07

Greg Robertson


Video Answer


1 Answers

It seems like this is not supported, but to make a workaround you could use:

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 isn't optimal but it should work without using deprecated classes/methods. You use repeats: false since you are intercepting the notification just before the user receives it and creating a new notification. Additionally you can use it in combination with UNNotificationAction and UNNotificationCategory if you handle multiple notifications.

like image 110
Gillsoft AB Avatar answered Sep 20 '22 05:09

Gillsoft AB