【问题标题】:SwiftUI willFinishLaunchingWithOptions has launchOptions = nilSwiftUI willFinishLaunchingWithOptions 有 launchOptions = nil
【发布时间】:2020-12-18 00:01:40
【问题描述】:

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

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

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 beta 6 和 Xcode beta 6。

提前致谢。

【问题讨论】:

  • 它不应该在那里。你会显示代码如何/在哪里设置委托? (顺便说一句,这个工作流程应该在设备上测试 - 它不适用于模拟器)。
  • 是的,我在真机而不是模拟器上测试。
  • @Asperi here is my AppDelegate 用我的 AppDelegate 设置更新了问题

标签: swiftui firebase-cloud-messaging ios14


【解决方案1】:

您可以通过 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()
        }
    }

[更新] 即使应用程序未启动,您也可以收到来自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()
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2020-04-03
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多