Managing local notifications

I created this code that sends local notification after user reaches the view controller, but the notification is sent each time the user goes to the view controller. How do I make it send the notification only once? What change should I make in my code?

import UserNotifications
class firstViewController: UIViewController, UNUserNotificationCenterDelegate {
override func viewDidLoad() {
    super.viewDidLoad()

            // Do any additional setup after loading the view.
            
            
            let center = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.badge,.sound,.alert]) { granted, error in
                if error == nil {
                    print("User permission is granted : \(granted)")
                }
          }
        //        Step-2 Create the notification content
                let content = UNMutableNotificationContent()
                content.title = "Hello"
                content.body = "Welcome"
           
            
        //        Step-3 Create the notification trigger
                let date = Date().addingTimeInterval(2)
                let dateComponent = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
                let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: false)
            
            
            
        //       Step-4 Create a request
                let uuid = UUID().uuidString
                let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
                
            
        //      Step-5 Register with Notification Center
                center.add(request) { error in
            
            
                }
        }

        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            completionHandler([.sound,.banner,.badge])
        }

    }

the notification is sent each time the user goes to the view controller.

The notification is scheduled in viewDidLoad.
This suggests that when you leave firstViewController, it is destroyed.
So when you re-visit it, it is recreated, and the notification is scheduled again.

You don't include the supporting code, showing how firstViewController is instantiated... ... so you could either:

  • Keep firstViewController in memory, so it is not repeatedly created and destroyed

or

  • Save a Bool flag somewhere permanent (e.g. in your Data Model, or in the calling ViewController), to record that the notification has been scheduled (and test this flag, before scheduling the notification)

Tip: the class "firstViewController" should really be named with a capital first letter: "FirstViewController"

Managing local notifications
 
 
Q