Parthib Biswas
Parthib Biswas

Reputation: 501

Triggering all pending notifications before the time set for it

In the app I'm developing, there is an option for triggering notification x amount of time before the actual time set for the said notification. For example I set the reminder for 10:00. But in the app's local settings, I set the notification to trigger 10 minutes before the time set. So, in this example's case, the notification will trigger in 9:50.

Now, I can do the above when I'm setting time for individual notification. But what I want to do is trigger all pending notifications before the actual time set for it.

This is the function I'm using to set notifications:

func scheduleNotification(at date: Date, identifier: String, threadIdentifier: String, body: String) {
    let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, year: components.year, month: components.month, day: components.day, hour: components.hour, minute: components.minute)

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

    let content = UNMutableNotificationContent()
    content.title = "TestNotification"
    content.body = body
    content.threadIdentifier = threadIdentifier
    content.sound = UNNotificationSound.default()

    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) {
        error in

        if let error = error {
            print("Error in delivering notification. Error: \(error.localizedDescription)")
        }
    }
}

The date is coming from the date set by the date picker. I tried to change trigger properties by using this code:

UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in
    for request in requests {
            request.trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10*60, repeats: false)
    }
}

But now I get an error saying 'trigger' is a get-only property.

Upvotes: 0

Views: 534

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

There is no way to change fire time of a scheduled notification , you can remove all of them and re-schedule again

Upvotes: 1

Related Questions