无法转换 __NSCFString 类型的值

Could not cast value of type __NSCFString

我正在试用 FCM 消息服务。我已经设置了一切完善。当我收到来自 FCM 的消息时,出现以下错误。我不确定这里出了什么问题:

AnyHashable("google.c.a.c_id"): 1586592282379086000, AnyHashable("gcm.message_id"): 0:1515418580276498%f4373656f4373656]

Could not cast value of type '__NSCFString' (0x1b4c70040) to 'NSDictionary' (0x1b4c70b80).

2018-01-08 19:06:21.198548+0530 Ozone[1067:540436] Could not cast value of type '__NSCFString' (0x1b4c70040) to 'NSDictionary' (0x1b4c70b80).

奇怪的是我创建了一个单独的推送通知应用程序并放置了相同的代码,它在那里工作。这里可能有什么问题?

这是片段代码:我在 let d 中收到错误:[String : Any].....line

UNNotificationPresentationOptions) -> Void) {  
           print("Handle push from foreground\(notification.request.content.userInfo)")  

            let dict = notification.request.content.userInfo["aps"] as! NSDictionary  
            let d : [String : Any] = dict["alert"] as! [String : Any]  
            let body : String = d["body"] as! String  
            let title : String = d["title"] as! String  
            print("Title:\(title) + body:\(body)")  
            self.showAlertAppDelegate(title: title,message:body,buttonTitle:"ok",window:self.window!)  

}  

有人可以帮我解决这个问题吗?我知道这是一个非常普遍的错误,但不确定为什么会出现这种情况,因为它在另一个应用程序中运行良好。

谢谢!

alert 键可以是字符串或字典(取决于您的服务器)。你可以试试这个。

if let dict = notification.request.content.userInfo["aps"] as? [String : Any] {

    if let d = dict["alert"] as? [String : Any],
        let body = d["body"] as? String,
        let title = d["title"] as? String {

            print("Title:\(title) + body:\(body)")
    } else if let body = dict["alert"] as? String {

        print("body:\(body)")
    }
}

而且你应该避免强制转换并且不要在 swift 中使用 NSDictionary