1

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.

karmjit singh
  • 229
  • 3
  • 14

1 Answers1

1

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()
    }
user12208004
  • 1,704
  • 3
  • 15
  • 37