WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Notification

Forums > Swift

Hello everyone, After scheduling a notification, I'm trying to get the trigger date but I don't know if it's possible and how to do it. Can anyone help me? Thank you.

    func scheduleNotification() {
                let content = UNMutableNotificationContent()
                content.title = "Hello"
                content.subtitle = "World"
                content.sound = UNNotificationSound.default

                let dateComponents = Calendar.current.dateComponents([.day, .year, .month, .hour, .minute], from: Date())

                // I'm trying to get dateMatching in getNotificationDateComponents()
                let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

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

                UNUserNotificationCenter.current().add(request)
        }

    func getNotificationDateComponents() -> DateComponents {
        let center = UNUserNotificationCenter.current()
        var dateComponents = DateComponents()

        center.getPendingNotificationRequests { requests in
           let request = requests.first { request in
               request.identifier == "myID"
            }

            let requestTrigger = request?.trigger
            // how can I get the trigger date?
        }

        return dateComponents
    }

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.