【问题标题】:How To Move To Spesific Screen iOS When User Tap Notification FCM当用户点击通知 FCM 时如何移动到特定屏幕 iOS
【发布时间】:2021-02-18 09:36:11
【问题描述】:

我做了一个通知,现在我的通知已经出现在 iOS 上,当我点击通知包时,通知出现我想切换到另一个 View Controller 页面。

这是我在 AppDelegate.swfit

中的代码
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        // 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 message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        
        
        // Print full message.
        print("user data msg \(userInfo)")
        
        
        
        guard let aps = userInfo["aps"] as? [String : AnyObject] else {
            print("Error parsing aps")
            return
        }
        print(aps)
        
        if let alert = aps["alert"] as? String {
            body = alert
        } else if let alert = aps["alert"] as? [String : String] {
            body = alert["body"]!
            title = alert["title"]!
        }
        
        if let alert1 = aps["category"] as? String {
            msgURL = alert1
        }
        
        print("Body\(body)")
        print(title)
        print(msgURL)
        
                let storyBoard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyBoard.instantiateViewController(withIdentifier: "register")
        
                vc.modalPresentationStyle = .overFullScreen
                present(vc, animated: true)
        
        
        
        
    }
    
    
}

但我收到错误:在范围内找不到“存在”当用户在 Swift 中收到通知时,我应该将导航代码放在哪里。

【问题讨论】:

  • 你有没有试过用 push 代替 present

标签: swift xcode push-notification firebase-cloud-messaging


【解决方案1】:

present() 是UIViewController 上的一个方法,因此您必须引用一个可以调用该方法的视图控制器。

这可能因应用的结构而异 - 特别是如果您使用的是 SceneDelegate、您有多个窗口等。

我的建议是你发布一个Notification,你可以在任何引用你的顶视图控制器的地方收听。例如,代替present,您可以执行以下操作:

let dataDict : [String : UIViewController] = ["vc": vc]
NotificationCenter.default.post(name: Notification.Name("ShowVC"), object: nil, userInfo: dataDict)

然后,假设您使用的是 SceneDelegate,您可以在 scene(_ scene: UIScene, willConnectTo) 函数中执行此操作:

NotificationCenter.default.publisher(for: Notification.Name("ShowVC"))
            .compactMap {
                $0.userInfo as? [String: UIViewController]
            }
            .sink {
                guard let vc = $0["vc"] else { 
                  return
                }
                window?.rootViewController?.present(vc, animated: true)
            }.store(in: &cancelSet)

在您的文件顶部,您需要import Combine,然后您的SceneDelegate 需要一个新属性才能使上述代码正常工作:

var cancelSet: Set<AnyCancellable> = []

即使您不使用SceneDelegate,这里的基本概念也应该适用于其他场景。

但是请注意,这不是一个万无一失的计划——根视图控制器必须是可见视图才能使其工作。这一切都可能取决于您的架构。在 SO 中搜索“topmost view controller”以查看有关此主题的大量讨论。

【讨论】:

  • 你有那个兄弟/先生的参考或完整代码,因为我在文档中读到了 firebase 没有提供。我有点困惑。
  • Firebase 不提供什么?
  • 提供用户点击和移动屏幕时的句柄文档。以及如何发送消息标题和正文,我已经解析并想发送到我的 ViewController?,你知道吗?谢谢。
  • 当用户点击时,您已经在处理。我已经提供了如何基于此移动到另一个屏幕的代码。关于将参数发送到 ViewController,我没有您的“注册”视图控制器的代码,所以我不知道它的结构。当您将 vc 实例化为特定的视图控制器类型然后在其上设置属性时,您可能需要强制转换它。
  • 这里有很多从情节提要中实例化 ViewController 并传递参数的示例:stackoverflow.com/questions/30449137/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多