Arvind Chourasiya
Arvind Chourasiya

Reputation: 17412

Xamarin iOS: Unable to start new ViewController by tapping on remote notifications

I am working on iOS, implemented FCM remote notifications. When i am tapping notification(when app open) it is not starting another screen. See below code i am using.

Here i am opening ChildrenViewController when app start by clicking on it

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {  

            Window = new UIWindow(UIScreen.MainScreen.Bounds);
            UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;

                 var cvc = new ChildrenViewController();
                 cvc.childs = SISConst.Mychildren;
                 this.navCntl = new UINavigationController(cvc);
                 this.navCntl.NavigationBarHidden = true;
                 Window.MakeKeyAndVisible();
                 SetRootViewController(this.navCntl, false);
         }

public void SetRootViewController(UIViewController rootViewController, bool animate)
        {
            if (animate)
            {
                var transitionType = UIViewAnimationOptions.TransitionFlipFromRight;
                if (Window == null)
                {
                    Window = new UIWindow(UIScreen.MainScreen.Bounds);
                }
                Window.RootViewController = rootViewController;
                UIView.Transition(Window, 0.5, transitionType,
                                  () => Window.RootViewController = rootViewController,
                                  null);
            }
            else
            {
                Window.RootViewController = rootViewController;
            }
        }

On notifications click i am using separate nested class CustomUNUserNotificationCenterDelegate which is not starting ChildrenViewController with updated data

     public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
        {
            public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
                    {
                        completionHandler();
                        var cvc = new ChildrenViewController();
                        cvc.studentId=studentId;
                        cvc.moduleName = moduleName;
                        cvc.isPush = true;
                        //var nv = new UINavigationController();
                        var appdelegate=UIApplication.SharedApplication.Delegate as AppDelegate;               
                        //appdelegate.Window.RootViewController = nv; 
                        var nv = appdelegate.Window.RootViewController as UINavigationController;                      
                        nv.PushViewController(cvc, false);         
                    }
        }

ViewDidLoad() of ChildrenViewController here i am checking whether app start by click or by tapping notification so that i am assigning different data

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    this.ShowStatusBarWithWhiteBg();
    this.DesignScreenTitleAs("");
    this.DesignLogoAppTitle();

    this.AutomaticallyAdjustsScrollViewInsets = false;

    if (isPushNotification)
    {            
        ShowDashboardScreen();
    }
    else
    {             
        ShowChilds();
    }
}

Upvotes: 1

Views: 396

Answers (1)

iamyogish
iamyogish

Reputation: 2432

In the delegate method DidReceiveNotificationResponse, take a look at the code,

var nv = new UINavigationController();
nv.PushViewController(cvc, false); 

What you're doing here is, you're creating a new Navigation controller and trying to push children view controller to this newly created navigation controller stack. Problem here is, the navigation controller you created here is not set as root view controller of the window.

What you could do is, set this newly create navigation VC as your root view controller if that's what you want to do or get the reference of root view controller of the window which is a navigation controller as I see from your code and try to push the child view controller on it.

Hope this helps.

Upvotes: 1

Related Questions