Bob
Bob

Reputation: 4386

Xamarin/Mvvmcross: Open a different view controller when a iOS push notification is received

I have found this question asked using Objective-c but I am unable to translate it into C#

e.g. open-specific-view-when-opening-app-from-notification

Basically I want to do this:

public override void ReceivedRemoteNotification (UIApplication application, NSDictionary userInfo)
    {
        string alert = (aps[new NSString("alert")] as NSString).ToString();
        Debug.WriteLine ("I want to open a specific ViewController and pass in my alert");
    }

I am actually using mvvmcross to manage my View navigation. So ideally I want to somehow implement this navigation using mvvmcross. In mvvmcross I would navigate to my ViewControler by doing this:

            this.ShowViewModel<SpecificControllerViewModel>();

Thanks

Upvotes: 1

Views: 1249

Answers (1)

WriteEatSleepRepeat
WriteEatSleepRepeat

Reputation: 3141

if you look to the parameters of ShowViewModel(), it has a way to pass values to the view-model

this is described here along with examples

You can achieve that in few ways. You could use a custom message. A view-model can register to receive the message and you send it from the ReceivedRemoteNotification Read here about messenger in MvvmCross.

Or, you can call ShowViewModel. If you look to how ShowViewModel is implemented here, it uses a IMvxViewDispatcher singleton service, so you could have the following utility method:

    static void ShowViewModel<T>(object parameter) where T : IMvxViewModel
    {
        var viewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
        var request = MvxViewModelRequest.GetDefaultRequest(typeof(T));
        request.ParameterValues = ((object)parameter).ToSimplePropertyDictionary();
        viewDispatcher.ShowViewModel(request);
    }

I posted about this on my blog here.

I think the 2nd way can work even in the case when the notification is received when app is not running (received by FinishedLaunching)

Upvotes: 1

Related Questions