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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.