SwiftUI willFinishLaunchingWithOptions has launchOptions = n



我已经为Firebase通知注册了应用程序,并且我已经实现了UNUserNotificationCenterDelegate。当应用程序正在运行或在后台时调用CCD_ 2方法。

当我终止应用程序并发送通知时。调用了willFinishLaunchingWithOptionsdidFinishLaunchingWithOptions方法,但launchOptions对象为nil,并且不包含任何.remoteNotificationton键。

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

我的AppDelegate设置


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 
@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate 
extension AppDelegate: MessagingDelegate

上面的代码从未将任何日志消息打印到控制台。

我尝试了所有的东西,并在谷歌上搜索了全部,但没有具体的东西。

我使用的是iOS 14测试版6和Xcode测试版6。

提前谢谢。

您可以使用connectionOptions.notificationResponse从SceneDelegate而非AppDelegate获得通知

发件人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]即使应用程序未启动,也可以从userNotificationCenter获得通知

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
print("open notification")
let userInfo = response.notification.request.content.userInfo

print(userInfo)
completionHandler()
}

相关内容

  • 没有找到相关文章