Handling Push Notifications in the app

Hello there, I am a neophyte iOS developer and I am still learning to build iOS apps.
My questions is, how to handle push notifications sent by a third party software (Eg: Firebase) ?
My question can further be split into to two parts. Part one is how to handle the notification to open a particular view when the app is in foreground? And second part is similar to the first part but the only difference is handling the notification, when the app is in background?
I have read couple to articles to handle push notifications but none of it really provided reasonable solutions to my app. I am curious to know about the life cycle of the app as well, because it has to be understood carefully for implementing background notification handling.
Hence it is a request to all the experienced iOS developers to help me understand handling notifications. Thanks.

Replies

To handle a notification, you have to:

  • register to this notification (in general in viewDidLoad), by adding an observer
        NotificationCenter.default.addObserver(self, selector: #selector(someFunc), name:.kNotif, object: nil)  // you need to know the name of notification
  • The name is defined by the one who post it (here Firebase), in general as
extension Notification.Name { 
    public static let kNotif = Notification.Name("someName") 
}
  • You define the func as:
    @objc func someFunc() {      
        // Do what you need here
   }