0

Currently, this is my code-snippet, in handling tap action, of local notification.

The following code will launch a UIViewController, when user taps on the local notification.

import UIKit

extension UIApplication {
    //
    // https://stackoverflow.com/a/58031897/72437
    //
    var keyWindow: UIWindow? {
        // Get connected scenes
        UIApplication
            .shared
            .connectedScenes
            .compactMap { $0 as? UIWindowScene }
            .flatMap { $0.windows }
            .first { $0.isKeyWindow }
    }
    
}

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
        UNUserNotificationCenter.current().delegate = self
        return true
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    // This method is called when user clicked on the notification
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
    {
        let newViewController = NewViewController.instanceFromNib()
        newViewController.postInit(response.notification.request.identifier)
        
        UIApplication.shared.keyWindow?.rootViewController?.present(newViewController, animated: false, completion: nil)
        
        completionHandler()
    }
}

What I am puzzling is, should I install UNUserNotificationCenterDelegate during willFinishLaunchingWithOptions, or didFinishLaunchingWithOptions?

Install during willFinishLaunchingWithOptions

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    return true
}

Install during didFinishLaunchingWithOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().delegate = self
    return true
}

Based on https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate

You must assign your delegate object to the UNUserNotificationCenter object before your app finishes launching. For example, in an iOS app, you must assign it in the application(:willFinishLaunchingWithOptions:) or application(:didFinishLaunchingWithOptions:) method of your app delegate. Assigning a delegate after the system calls these methods might cause you to miss incoming notifications.

It seems like there is no difference between willFinishLaunchingWithOptions and didFinishLaunchingWithOptions

I was wondering, do you find any differences, between willFinishLaunchingWithOptions and didFinishLaunchingWithOptions, as far as UNUserNotificationCenterDelegate installation is concerned?

Thanks.

Cheok Yan Cheng
  • 48,324
  • 124
  • 436
  • 828
  • You have to set the delegate **before** using it, in case of remote notifications as soon as possible, in case of local notifications the location doesn't matter if in `will` or `did`. – vadian May 24 '22 at 09:03

0 Answers0