skip to Main Content

My problem is that I am trying to schedule notifications that come daily at a specific time this is my code

import SwiftUI

struct notifView: View {
    var body: some View {
        VStack {
            VStack {
                Button("Request Permission") {
                    let center = UNUserNotificationCenter.current()

                    center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
                        if granted {
                            print("Access Granted!")
                        } else {
                            print("Access Not Granted")
                        }
                    }
                }
                .frame(width: 200, height: 60, alignment: .center)
                .foregroundColor(.black)
                .background(Color.blue)
                .cornerRadius(10.0)
                .padding()
                Button("Add Notifications For Morning") {
                    func scheduleNotification() {
                        let center = UNUserNotificationCenter.current()

                        let content = UNMutableNotificationContent()
                        content.title = "Morning Time"
                        content.body = "Wake Up And Be Productive!"
                        content.categoryIdentifier = "reminder"
                        content.sound = UNNotificationSound.default

                        var dateComponents = DateComponents()
                        dateComponents.hour = 6
                        dateComponents.minute = 30
                        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

                        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
                        center.add(request)
                    }
                }
                .padding()
                Button("Add Notifications For Middle Of The Day") {
                    func scheduleNotification() {
                        let center = UNUserNotificationCenter.current()

                        let content = UNMutableNotificationContent()
                        content.title = "Middle Of The Day"
                        content.body = "Did you have your daily run?"
                        content.categoryIdentifier = "reminder"
                        content.sound = UNNotificationSound.default

                        var dateComponents = DateComponents()
                        dateComponents.hour = 12
                        dateComponents.minute = 30
                        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

                        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
                        center.add(request)
                    }
                }
                .padding()
                Button("Add Notifications For Night") {
                    func scheduleNotification() {
                        let center = UNUserNotificationCenter.current()

                        let content = UNMutableNotificationContent()
                        content.title = "Night Time"
                        content.body = "Time to sleep"
                        content.categoryIdentifier = "reminder"
                        content.sound = UNNotificationSound.default

                        var dateComponents = DateComponents()
                        dateComponents.hour = 20
                        dateComponents.minute = 51
                        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

                        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
                        center.add(request)
                    }
                }
                .foregroundColor(.blue)
                .padding()
            }
        }
    }
}

struct notifView_Previews: PreviewProvider {
    static var previews: some View {
        notifView()
    }
}

3

Answers


  1. Chosen as BEST ANSWER

    I got it working with the help of this video: https://www.youtube.com/watch?v=mG9BVAs8AIo

    and some code from @lorem ipsum thank you so much for the help

    import SwiftUI
    import UserNotifications
    
    struct notifView: View {
        var body: some View {
            VStack {
                Button("Request Permission") {
                    NotificationManager.instance.requestAuthorization()
                }
                .padding()
                .background(Color.green)
                .cornerRadius(10)
                Button("1st Notification") {
                    
                    var dateComponents = DateComponents()
                    dateComponents.hour = 4
                    dateComponents.minute = 01
    
    // reusable code thanks to lorem ipsum
                    
                  NotificationManager.instance.scheduleTriggerNotification(title: "1st Notification", body: "1st Notification Body", categoryIdentifier: UUID().uuidString, dateComponents: dateComponents, repeats: true)
                }
                .padding()
                .background(Color.yellow)
                .cornerRadius(10)
                Button("2nd Notification") {
                    
                    var dateComponents = DateComponents()
                    dateComponents.hour = 5
                    dateComponents.minute = 40
    
    // reusable code thanks to lorem ipsum
                    
                    NotificationManager.instance.scheduleTriggerNotification(title: "2nd Notification", body: "2nd Notification Body", categoryIdentifier: UUID().uuidString, dateComponents: dateComponents, repeats: true)
                }
                .padding()
                .background(Color.yellow)
                .cornerRadius(10)
                Button("3rd Notification") {
                    
                    var dateComponents = DateComponents()
                    dateComponents.hour = 12
                    dateComponents.minute = 10
    
    // reusable code thanks to lorem ipsum
                    
                    NotificationManager.instance.scheduleTriggerNotification(title: "3rd Notification", body: "3rd Notification Body", categoryIdentifier: UUID().uuidString, dateComponents: dateComponents, repeats: true)
                }
                .padding()
                .background(Color.yellow)
                .cornerRadius(10)
            }
        }
    }
    
    class NotificationManager {
        
        static let instance = NotificationManager()
        
        func requestAuthorization() {
            let options: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(options: options) { (success, error) in
                if let error = error {
                    print("ERROR: (error)")
                } else {
                    print("SUCCESS")
                }
            }
        }
        // reusable code thanks to lorem ipsum
        func scheduleTriggerNotification(title: String, body: String, categoryIdentifier: String, dateComponents : DateComponents, repeats: Bool) {
                print(#function)
                let content = UNMutableNotificationContent()
                content.title = title
                content.body = body
                content.categoryIdentifier = categoryIdentifier
                content.sound = UNNotificationSound.default
                
                let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: repeats)
                
                let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request)
            }
        
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            notifView()
        }
    }
    

  2. Look at the comments within the code

    import SwiftUI
    //struct and class should start with an uppercase
    struct NotificationView: View {
        //Central location for Notification code including the delegate
        // A call to the notificationManager just like the line of code below has to be included in
        // application(_:willFinishLaunchingWithOptions:) or
        // application(_:didFinishLaunchingWithOptions:)
        //https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
        //https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-an-appdelegate-to-a-swiftui-app
        let notificationManager: NotificationManager = NotificationManager.shared
        var body: some View {
            VStack {
                VStack {
                    Button("Request Permission") {
                        //Call a func here don't define it
                        notificationManager.requestAuthorization()
                    }
                    .frame(width: 200, height: 60, alignment: .center)
                    .foregroundColor(.black)
                    .background(Color.blue)
                    .cornerRadius(10.0)
                    .padding()
                    Button("Add Notifications For Morning") {
                        //Unique date components
                        var dateComponents = DateComponents()
                        dateComponents.hour = 6
                        dateComponents.minute = 30
                        //Reusable method
                        self.notificationManager.scheduleTriggerNotification(title: "Morning Time", body: "Wake Up And Be Productive!", categoryIdentifier: "reminder", dateComponents: dateComponents, repeats: true)
                    }
                    .padding()
                    Button("Add Notifications For Middle Of The Day") {
                        var dateComponents = DateComponents()
                        dateComponents.hour = 12
                        dateComponents.minute = 30
                        //Reusable method
                        self.notificationManager.scheduleTriggerNotification(title: "Middle Of The Day", body: "Did you have your daily run?", categoryIdentifier: "reminder", dateComponents: dateComponents, repeats: true)
                        
                    }
                    .padding()
                    Button("Add Notifications For Night") {
                        var dateComponents = DateComponents()
                        dateComponents.hour = 20
                        dateComponents.minute = 51
                        //Reusable method
                        self.notificationManager.scheduleTriggerNotification(title: "Night Time", body: "Time to sleep", categoryIdentifier: "reminder", dateComponents: dateComponents, repeats: true)
                        
                    }
                    .foregroundColor(.blue)
                    .padding()
                    
                    Button("Print Notifications") {
                        //Reusable method
                        self.notificationManager.printNotifications()
                    }
                    .foregroundColor(.blue)
                    .padding()
                    Button("Delete Notifications") {
                        //Reusable method
                        self.notificationManager.deleteNotifications()
                    }
                    .foregroundColor(.blue)
                    .padding()
                }
            }
        }
    }
    //You need a central location for the notification code because
    // it is needed in more than 1 spot. At launch in the AppDelegate
    // and wherever you schedule your notifications
    class NotificationManager: NSObject, UNUserNotificationCenterDelegate{
        //Singleton is requierd because of delegate
        static let shared: NotificationManager = NotificationManager()
        let notificationCenter = UNUserNotificationCenter.current()
        
        private override init(){
            super.init()
            //This assigns the delegate
            notificationCenter.delegate = self
        }
        
        func requestAuthorization() {
            print(#function)
            notificationCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
                if granted {
                    print("Access Granted!")
                } else {
                    print("Access Not Granted")
                }
            }
        }
        
        func deleteNotifications(){
            print(#function)
            notificationCenter.removeAllPendingNotificationRequests()
        }
        ///This is just a reusable form of all the copy and paste you did in your buttons. If you have to copy and paste make it reusable.
        func scheduleTriggerNotification(title: String, body: String, categoryIdentifier: String, dateComponents : DateComponents, repeats: Bool) {
            print(#function)
            let content = UNMutableNotificationContent()
            content.title = title
            content.body = body
            content.categoryIdentifier = categoryIdentifier
            content.sound = UNNotificationSound.default
            
            let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: repeats)
            
            let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
            notificationCenter.add(request)
        }
        ///Prints to console schduled notifications
        func printNotifications(){
            print(#function)
            notificationCenter.getPendingNotificationRequests { request in
                for req in request{
                    if req.trigger is UNCalendarNotificationTrigger{
                        print((req.trigger as! UNCalendarNotificationTrigger).nextTriggerDate()?.description ?? "invalid next trigger date")
                    }
                }
            }
        }
        //MARK: UNUserNotificationCenterDelegate
        func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            
            completionHandler(.banner)
        }
    }
    struct NotificationView_Previews: PreviewProvider {
        static var previews: some View {
            NotificationView()
        }
    }
    
    Login or Signup to reply.
  3. Add one day in today’s date so we got next day and then add your specific time on which you want trigger the notification.

    var triggerComponents = DateComponents()
    
    let nextDay = Calendar.current.date(byAdding: .day, value: 1, to: Date()) ?? Date()
    triggerComponents = Calendar.current.dateComponents([.year, .month, .day, .hour], from: nextDay)
    triggerComponents.hour = 14
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search