在 AppDelegate iOS 中单击 FCM 推送通知时导航到特定选项卡 - Swift
Posted
技术标签:
【中文标题】在 AppDelegate iOS 中单击 FCM 推送通知时导航到特定选项卡 - Swift【英文标题】:Navigating to specific tab when clicking on FCM push notification in AppDelegate iOS - Swift 【发布时间】:2021-04-01 00:54:59 【问题描述】:为了让它发挥作用,我绞尽脑汁想了一天。我收到了通知,但在单击时从未导航到特定选项卡。
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey]
print(tag + "Message ID: \(messageID)")
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
print(userInfo)
// let myTabBar = self.window?.rootViewController as? UITabBarController
// myTabBar?.selectedIndex = 2
//TabBarController - StoryBoard Id
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let tabController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? UITabBarController
tabController.selectedIndex = 2
completionHandler()
【问题讨论】:
【参考方案1】:对于使用 Swift 的 ios 14.4。 在其他问题的帮助下,这最终对我有用。
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey]
print(tag + "Message ID: \(messageID)")
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
print(userInfo)
let scene = UIApplication.shared.connectedScenes.first
if let sceneDelegate = scene?.delegate as? SceneDelegate
if let tabController = sceneDelegate.window?.rootViewController as? UITabBarController
tabController.selectedIndex = 2
print("Clicked Notification")
completionHandler()
如果您需要有关 FCM 推送通知的更多帮助,请告诉我。祝大家编码愉快!
【讨论】:
这很好用,我喜欢你从 uiapplication 中提取它的方式。 selectedIndex 2 的 ViewController 是一个导航控制器,但我在执行 pushViewController 时遇到问题。有什么想法吗?我要显示的视图是在 tabController.selectedIndex 2 中显示的列表视图上推送的详细视图 @Maddocks 我不太明白你的问题。当我有时间时,我需要查看一些代码来帮助你。希望你解决你的问题伙伴。tabController.selectedIndex = 2 guard let vc = tabController.selectedViewController as? UINavigationController else return vc.popToRootViewController(animated: true) DetailsVC = uistoryboard.init(...).instantiateViewController(...) DetailsVC.datasource = ... vc.pushViewController(DetailsVC, animated: true) // this is how i work with a tab view and a nav view
【参考方案2】:
对于导航到特定的 ViewController,我使用了这个扩展
public extension UIApplication
class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
if let nav = base as? UINavigationController
return getTopViewController(base: nav.visibleViewController)
else if let tab = base as? UITabBarController, let selected = tab.selectedViewController
return getTopViewController(base: selected)
else if let presented = base?.presentedViewController
return getTopViewController(base: presented)
return base
那就叫它如下:
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
let userInfo = response.notification.request.content.userInfo
// Print message ID.
if let messageID = userInfo[gcmMessageIDKey]
print(tag + "Message ID: \(messageID)")
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// Print full message.
print(userInfo)
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "yourStoryboardId") as! YourViewcontroller
// vc.nNotificationID = notificationID
if let topVC = UIApplication.getTopViewController()
topVC.navigationController?.pushViewController(vc, animated: false)
print("Clicked Notification")
completionHandler()
【讨论】:
我已经解决了我的问题。我还没有尝试过你的代码,但它看起来很有希望。感谢您的努力。以上是关于在 AppDelegate iOS 中单击 FCM 推送通知时导航到特定选项卡 - Swift的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ios 中单击通知时接收数据,当应用程序被杀死时,(react-native-fcm 模块在 IOS 中获取推送通知)