在 iOS 10 下使用 FCM 在前台单击推送通知后无法导航到特定的 viewController
Posted
技术标签:
【中文标题】在 iOS 10 下使用 FCM 在前台单击推送通知后无法导航到特定的 viewController【英文标题】:Unable to navigate to specific viewController after clicking push notification in foreground using FCM below iOS 10 【发布时间】:2018-05-08 09:34:28 【问题描述】:我正在尝试在前台和后台获取通知,我在两种状态下都收到通知,但是当应用程序处于前台时点击通知后,我无法将其导航到所需的视图控制器,任何帮助将不胜感激,在此先感谢 我正在使用第三方自定义横幅,在 iphone 4s 设备、ios 9 上运行
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
Messaging.messaging().appDidReceiveMessage(userInfo)
print("In did Recieve Notification")
// Print message ID.
print("userInfoNotification=\(userInfo)")
if let contactID = userInfo["contactID"] as? String
self.contactID = contactID
print(contactID)
let state = UIApplication.shared.applicationState
if state == .active
print("App in Foreground")
if self.contactID != AppDelegate.openedChatContactId
print("openedID=\(AppDelegate.openedChatContactId)")
if let aps = userInfo["aps"] as? NSDictionary
if let alert = aps["alert"] as? NSDictionary
let body = alert["body"] as! String
let title = alert["title"] as! String
let banner = Banner(title: title, subtitle: body, image: UIImage(named: "AppIcon"), backgroundColor: UIColor(red:31.00/255.0, green:136.0/255.0, blue:254.5/255.0, alpha:1.000))
banner.dismissesOnTap = true
banner.show(duration: 3.0)
// let storyboard = UIStoryboard(name: "Main", bundle: nil)
// let viewController = storyboard.instantiateViewController(withIdentifier: "chatMessageVC") as! ChatMessagesVC
// UIApplication.shared.keyWindow?.rootViewController = viewController;
else if let alert = aps["alert"] as? NSString
if state == .inactive || state == .background
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var destinationViewController = storyboard.instantiateViewController(withIdentifier: "chatMessageVC") as! ChatMessagesVC
UserDefaults.standard.set(contactID, forKey: "contactID")
UserDefaults.standard.synchronize()
destinationViewController.contactID = self.contactID
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(destinationViewController, animated: false)
【问题讨论】:
你有故事板和视图控制器吗?是navigationController还是tabbarController类型的app? @PPL 它是一个导航控制器类型的应用程序 在下面查看我的答案 这个方法被调用了吗? @kunalkushwaha 希望你也可以投票,这可能对其他人有所帮助 【参考方案1】:在UIApplication
的扩展下面实现
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
请更改您的代码以在应用处于前台时导航到特定屏幕。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "chatMessageVC") as! ChatMessagesVC
UIApplication.topViewController()?.navigationController?.pushViewController(viewController, animated: false)
希望这对您有帮助,如有任何疑问,请告诉我。
更新
如果应用程序在前台中运行,iOS 将不会显示通知横幅/警报。这是设计使然。但是我们可以使用 UILocalNotification 来实现它,如下所示
if application.applicationState == .active
var localNotification = UILocalNotification()
localNotification.userInfo = userInfo
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertBody = message
localNotification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(localNotification)
【讨论】:
它想在前台点击推送通知时导航到 ChatMessageVC。 收到通知时会自动导航到 ChatMessageVC。 会因为用户处于前台状态 如果应用程序在前台,它不会显示警报或横幅,如果你想显示它,请查看我的更新代码 我使用了第三方,我可以在前台显示推送通知,但在点击它时无法导航。以上是关于在 iOS 10 下使用 FCM 在前台单击推送通知后无法导航到特定的 viewController的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序处于前台时,FCM 处理 IOS 通知,这是我不想
在 AppDelegate iOS 中单击 FCM 推送通知时导航到特定选项卡 - Swift
如何在 ios 中单击通知时接收数据,当应用程序被杀死时,(react-native-fcm 模块在 IOS 中获取推送通知)