Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI willFinishLaunchingWithOptions has launchOptions = nil

I have the app registered for Firebase notifications and I have implemented the UNUserNotificationCenterDelegate. the didReceive method is called when the application is running or in the background.

When I terminate the application and send a notification. The willFinishLaunchingWithOptions and didFinishLaunchingWithOptions methods are called but launchOptions object is nil and does not contain any .remoteNotificaiton key.

if launchOptions != nil {
            logger.log("There is stuff in launch options")
        }

My AppDelegate Setup


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate 

extension AppDelegate: MessagingDelegate

The above code never prints any log message to the console.

I have tried everything and google for whole but nothing concrete.

I am on iOS 14 beta 6 and Xcode beta 6.

Thanks in advance.

like image 827
karmjit singh Avatar asked Nov 22 '25 06:11

karmjit singh


1 Answers

You can get notification from SceneDelegate not AppDelegate with connectionOptions.notificationResponse

From https://dev.to/nemecek_f/ios-13-launchoptions-always-nil-this-is-the-reason-solution-kki & https://stackoverflow.com/a/61322065/12208004

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
   if let notificationResponse = connectionOptions.notificationResponse {
          // you can get notification here
        }        

        let contentView = ContentView()

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = window
            window.makeKeyAndVisible()
        }
    }

[UPDATE] You can get notification from userNotificationCenter even when the app is not started

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        print("open notification")
        let userInfo = response.notification.request.content.userInfo
        
        print(userInfo)
        completionHandler()
    }
like image 139
user12208004 Avatar answered Nov 24 '25 15:11

user12208004



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!