1

I made a notification, now my notification has appeared on iOS when I tap the notifications bagde the notification appears I want to switch to another View Controller page.

This my code in 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)
        
        
        
        
    }
    
    
}

But I got error: Cannot find 'present' in scope Where I should put my code for naviagtion when the user got a notification in Swift.

zidniryi
  • 1,212
  • 3
  • 15
  • 36

1 Answers1

0

present() is a method on UIViewController, so you'll have to have a reference to a view controller that you can call that method on.

This can vary by the structure of your app -- especially if you're using a SceneDelegate, you have multiple windows, etc.

My suggestion is that you post a Notification that you can listen for in whatever has a reference to your top view controller. For example, instead of where you have present, you could do something like this:

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

Then, assuming you're using a SceneDelegate, you could do this in your scene(_ scene: UIScene, willConnectTo) function:

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)

At the top of your file you'll need to import Combine and then your SceneDelegate will need a new property in order for the above code to work:

var cancelSet: Set<AnyCancellable> = []

Even if you aren't using a SceneDelegate, the basic concepts here should apply to other scenarios.

Note, however, that this is not a foolproof plan -- the root view controller has to be the visible view in order for this to work. This all may depend on your architecture. Search SO for "topmost view controller" to see plenty of discussion on this topic.

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Have you have a reference or full code for that bro/sir because I read in the documentation firebase not provide that. I little bit confused. – zidniryi Feb 18 '21 at 10:05
  • Firebase does not provide what? – jnpdx Feb 18 '21 at 10:06
  • provide the documentation about handle when user tap and move screen. And how to send the Message title and body, I already parsed and I want to send to my ViewController?, Did you know that? Thanks. – zidniryi Feb 18 '21 at 10:07
  • You are already handling when the user taps. I've provided the code for how to move to another screen based on that. Regarding sending the parameters to the ViewController, I don't have the code for your "register" view controller, so I don't know how it's structured. You'll probably need to cast `vc` when you instantiate it as a certain view controller type and then set properties on it. – jnpdx Feb 18 '21 at 10:11
  • Lots of examples here of instantiating ViewControllers from storyboard and then passing them parameters: https://stackoverflow.com/questions/30449137/custom-init-of-uiviewcontroller-from-storyboard – jnpdx Feb 18 '21 at 10:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/228889/discussion-between-jnpdx-and-zidniryi). – jnpdx Feb 18 '21 at 11:31