1
votes

I'm developing a messaging app and I'm having trouble implementing push notification behaviour.

What I want is that when a user taps on the notification, the conversation of that group opens. But there has to be a Navigation bar to go back to the list of contacts.

I already know how to extract information from the notification to get the contact name. I'm trying this in the function didReceive on AppDelegate.

So far what I've tried is:

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
vc.contactName = name as String
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyandVisible()

This open the right conversation and the messages appear, but UI elements are no responsive (can't write when tapping textfield, cant navigate to gallery when tapping button, etc.) and also there is no Navigation bar.

So after doing some research, I've tried this:

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
vc.contactName = name as String
let rootViewController = self.window?rootViewController as! UINavigationController
rootViewController. pushViewController(vc, animated: true)

This doesn't work. There is no error message but the screen is just white.

This is my Storyboard: enter image description here I'm using SWRevealViewController for the side menu and the messaging part has a tabBarController, with the contact list on the 2ยบ tab.

EDIT: With the second method, there is an error.

Could not cast value of type 'SWRevealViewController to UINavigationController'

2

2 Answers

1
votes

I think you should go with NotificationCenter to do this.

When the user click on the notification you should post a NotificationCenter in the didReceive like this:

NotificationCenter.default.post(name: .onMessageRecieved, object: notificationObject)

And based on which view controller you're in you should add an observer there in the viewDidLoad() func so it understands a message has been recieved like this:

override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.onMessageRecieved(notification:)), name: .onMessageRecieved, object: nil)

}

And you should have a func (onMessageRecieved) in the view controller to do the navigation like this:

    func onMessageRecieved(notification: NSNotification){

            // open the conversation

            let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController

            vc.contactName = name as String 

            self.navigationController?.pushViewController(vc, animated: true)

   }

This way you are doing the navigation from the view controller so the navigation will be there as well.

0
votes

Could not cast the value of type 'SWRevealViewController to UINavigationController'

This issue is because of the "SWRevealViewController" is not a subclass of UINavigationController. So you can't cast it this way.

To solve the chat ViewController issue, you should modify your code like this.

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
let nav = vc.navigationController
vc.contactName = name as String
let rootViewController = self.window?rootViewController as! UIViewController
rootViewController.presentViewController(nav, animated: true, completion: nil)

But the chat VC will be in the presenter mode. You should handle the dismiss.

You just try this and let me know is it working or not.