首次启动应用程序时,我在didReceiveRemoteNotification崩溃

时间:2019-07-18 09:14:08

标签: ios swift push-notification

我正在使用Firebase开发实时聊天应用程序,所以一切对我来说都很好,但是我按照通知类型直接重定向到viewcontroller,并且成功重定向,但是问题在于应用程序首次启动时时间比崩溃快,并得到如下错误

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

这是我的didReceiveRemoteNotification的代码

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        let notificationType = userInfo["notification_type"] as! String //i am getting crash here
        if notificationType == "news_notification"{
            let rootViewController = self.window?.rootViewController as! UINavigationController

            let storyboard = UIStoryboard(name: "Settings", bundle: nil)
            let VC = storyboard.instantiateViewController(withIdentifier: "NewsDetailViewController") as! NewsDetailViewController
            if let aps  = userInfo["aps"] as? NSDictionary{
                if let alert = aps["alert"] as? NSDictionary{
                    if let body = alert["body"] as? String{
                        print(body)
                        VC.name1 = body
                    }
                    if let title = alert["title"] as? String{
                        print(title)
                        VC.name = title
                    }
                }
            }
            VC.vcValue = "1"
            rootViewController.pushViewController(VC, animated: true)

        }
    }

如果我注释该代码,然后在登录后成功登录,则我会取消注释该代码,认为它可以正常工作,但只有首次启动,否则我报错

3 个答案:

答案 0 :(得分:2)

然后该键不存在,请使用guard

 print(userInfo) // print to see content 
 guard let notificationType = userInfo["notification_type"] as? String else { return }

答案 1 :(得分:1)

我认为您没有导航控制器,请尝试

guard let rootViewController = self.window?.rootViewController as? UINavigationController else { return }

答案 2 :(得分:1)

您被迫解开了零的东西,您正在做很多事情,并且如果只是在彼此的顶部放金字塔,这也是不好的做法。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let storyboard = UIStoryboard(name: "Settings", bundle: nil)

    guard let notificationType = userInfo["notification_type"] as? String,
        notificationType == "news_notification",
        let rootViewController = self.window?.rootViewController as? UINavigationController,
        let vc = storyboard.instantiateViewController(withIdentifier: "NewsDetailViewController") as? NewsDetailViewController,
        let aps  = userInfo["aps"] as? NSDictionary else {return}

    if let body = alert["body"] as? String{
        print(body)
        VC.name1 = body
    }
    if let title = alert["title"] as? String{
        print(title)
        VC.name = title
    }
    VC.vcValue = "1"
    rootViewController.pushViewController(VC, animated: true)
}

有一个清除的版本。遵循2条重要规则:

1:如果您不是100%确定该值将始终存在,请不要用!

强制将其取消包装。

2:如果ypu没有else条件,您最好还是使用警卫来避免金字塔。