How to do it...

  1. Open the same demo project and click on the AppDelegate.swift file.
  2. The first thing that we will do is enable the presentable of Push Notification if the app is in foreground and handle the tapping of the Push Notifications.
  3. Update the function of registerForPushNotifications to be like the following:
      func registerForPushNotifications(){ 
          let application = UIApplication.shared 
          let center = UNUserNotificationCenter.current() 
          center.delegate = self 
          center.requestAuthorization(options: 
            [.alert, .sound]) { (granted, error) in 
              // actions based on whether 
                notifications were authorized or not 
              print(granted) 
              print(error ?? "No error") 
             
          } 
          application.registerForRemoteNotifications() 
      } 
  1. At the end of the file, add the following extension ...

Get iOS Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.