Я работаю над приложением, которое включает в себя функцию мессенджера, проблема заключается в том, что каждый раз, когда я нажимаю на push-уведомление, оно направлено на само приложение, а не на само сообщение. Я считаю, что код правильный, возможно, это проблемабыстрой версии ....?
// 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
}
}