0

I'm trying to display a specific webView url when user click on a notification, but I'm not in capacity to handle click event when user receive and click to the notification.

Even the print isn't on my console, idk what I'm doing wrong

//AppDeletage.swift
// Require notification Authorization
class AppDelegate: UIResponder, UIApplicationDelegate {

   weak var viewController : ViewController?

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      // Override point for customization after application launch.
      registerForPushNotifications()
      
      // When the app launch after user tap on notification (originally was not running / not in background)
      if(launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil){
         print("Notification handle")
      }
         
      return true
   }

   func registerForPushNotifications() {
      UNUserNotificationCenter.current()
         .requestAuthorization(options: [.alert, .sound, .badge]) {
         [weak self] granted, error in
            
         print("Permission granted: \(granted)")

         self?.viewController?.loadRequestWithoutToken()
         
         guard granted else { return }
         self?.getNotificationSettings()
      }
   }

   // Get notification settings
   func getNotificationSettings() {
   UNUserNotificationCenter.current().getNotificationSettings { settings in
      print("Notification settings: \(settings)")
      guard settings.authorizationStatus == .authorized else { return }
      DispatchQueue.main.async {
         UIApplication.shared.registerForRemoteNotifications()
      }
   }
   }

   func application(
   _ application: UIApplication,
   didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
   ) {
         let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
         let token = tokenParts.joined()
         print("Device Token: \(token)")
      
      viewController?.loadRequest(for: token)

   }

   func application(
   _ application: UIApplication,
   didFailToRegisterForRemoteNotificationsWithError error: Error) {
         viewController?.loadRequestWithoutToken()
      print("Failed to register: \(error)")
   }
}

extension AppDelegate: UNUserNotificationCenterDelegate{
    
  // This function will be called right after user tap on the notification
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

     print("User has click on notification")

    let myURL = "https://app.jmt-alimentation-animale.com/index.php/news/single/80225"
    let URLWithToken = URL(string: myURL)
    let URLWebView = URLRequest(url: URLWithToken!)

    viewController?.webView.load(URLWebView)
    // tell the app that we have finished processing the user’s action / response
    completionHandler()
  }
}

If someone could help me to, at least, get the handle of notification tap, It will be great. Thanks

Logan Blangenois
  • 403
  • 2
  • 10

0 Answers0