我正在开发一个具有Messenger功能的应用程序,问题是每次我点击指向该应用程序本身而不是消息本身的推送通知时,我相信代码是正确的,也许这是一个问题快速版本....?
// AppDelegate
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let gcmMessageIDKey = "gcm.message_id"
if let messageID = userInfo[gcmMessageIDKey] {
print("messageID: \(messageID)")
}
connectToFirebase()
Messaging.messaging().appDidReceiveMessage(userInfo)
if let dictString = userInfo["gcm.notification.customData"] as? String {
print("dictString \(dictString)")
if let dict = convertToDictionary(text: dictString) {
if let uid = dict["userId"] as? String,
let fullname = dict["username"] as? String,
let email = dict["email"] as? String,
let profileImageUrl = dict["profileImageurl"] as? String {
let user = User(uid: uid, fullname: fullname, email: email, profileImageurl: profileImageUrl, status: "")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let chatVC = storyboard.instantiateViewController(withIdentifier: IDENTIFIER_CHAT) as! ChatViewController
chatVC.partnerUserName = user.fullname
chatVC.partnerId = user.uid
chatVC.partnerUser = user
guard let currentVC = UIApplication.topViewController() else {
return
}
currentVC.navigationController?.pushViewController(chatVC, animated: true)
}
}
}
扩展
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}