如何在用户点击通知 FCM 时移动到特定屏幕 iOS

How To Move To Spesific Screen iOS When User Tap Notification FCM

我发出了通知,现在我的通知已经出现在 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)
        
        
        
        
    }
    
    
}

但我收到错误:无法在范围 中找到 'present' 当用户在 Swift 中收到通知时,我应该将导航代码放在哪里。

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

这可能因您的应用程序结构而异——尤其是如果您使用 SceneDelegate,您有多个 windows,等等

我的建议是你 post 一个 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 {
                [=11=].userInfo as? [String: UIViewController]
            }
            .sink {
                guard let vc = [=11=]["vc"] else { 
                  return
                }
                window?.rootViewController?.present(vc, animated: true)
            }.store(in: &cancelSet)

在你的文件的顶部,你需要 import Combine 然后你的 SceneDelegate 将需要一个新的 属性 以便上面的代码工作:

var cancelSet: Set<AnyCancellable> = []

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

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