Track if notification was fired

Hi, I have this:


import SwiftUI
import UserNotifications

struct ContentView: View {
   
    var body: some View {
        VStack {
            Button("Request Permission") {
                RequestNotificationsAccess()
            }

            Button("Schedule Notification") {
                ScheduleNotification()
            }
        }
    }
}

func RequestNotificationsAccess() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
        if success {
            print("All set!")
        } else if let error = error {
            print(error.localizedDescription)
        }
    }
}

func ScheduleNotification() {
    let content = UNMutableNotificationContent()
    content.title = "Title:"
    content.subtitle = "some text"
    content.sound = UNNotificationSound.default

    // show this notification five seconds from now
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    // choose a random identifier
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

    // add our notification request
    UNUserNotificationCenter.current().add(request)
}


It works great but when I click multiple times on the button, the notification appears multiple times, how can I track if it was fired already and if so, don't display anything?


Thanks

In real life, you could have to fire several notifications, with different content.

So, any test should test content as well.


You could use state var.


Here is a light modification of your code just to show:


import SwiftUI
import UserNotifications
 
struct ContentView: View {
    @State var done: Bool = false

    var body: some View {
        VStack {
            Spacer()
            Button("Request Permission") {
                RequestNotificationsAccess()
            }
            Spacer()
            Button(action: {
                ScheduleNotification(AlreadyDone: self.done)
                self.done = true
                }) {
                    Text("Schedule Notification")
            }
            Spacer()
        }
    }
}
 
func RequestNotificationsAccess() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
        if success {
            print("All set!")
        } else if let error = error {
            print(error.localizedDescription)
        }
    }
}
 
func ScheduleNotification(AlreadyDone: Bool) {
    if AlreadyDone {
        print("Already done!")
        return
    }
    let content = UNMutableNotificationContent()
    content.title = "Title:"
    content.subtitle = "some text"
    content.sound = UNNotificationSound.default
  print("Request to send")
    // show this notification five seconds from now
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
 
    // choose a random identifier
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
 
    // add our notification request
    UNUserNotificationCenter.current().add(request)
}
Track if notification was fired
 
 
Q