1
votes

I have implemented a push notification feature for my Xamarin.Forms UWP app and i am able receive notifications and then pop up a toast. I am using the code below for this.

//This method is triggered when a user taps/clicks the notification
private void Toast_Activated(ToastNotification sender, object args)
{
    ToastActivatedEventArgs targs = (ToastActivatedEventArgs)args;
    var xml = sender.Content.GetXml();
    XmlDocument xDoc = new XmlDocument();
    xDoc.LoadXml(xml);

    XmlNodeList txts = xDoc.GetElementsByTagName("text");
    var sitid = txts[0].InnerText;
    var id = sitid.Substring(sitid.LastIndexOf(':') + 1);
    Debug.WriteLine("Id:" + id);
}

When a user clicks/taps the notification, I want to open a specific page from my PCL project and pass this id variable as an argument. How can i do this ? Any help would be appreciated.

1

1 Answers

1
votes

Use ServiceLocator or whatever other dependency injection framework you have to call your navigation service.

If you want to use the Xamarin Forms built in one see here - http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/

Basically you then define an interface of

public interface INavigationService
{
     void NavigateTo(String pageKey);
}

Then you create a new class of

public class NavigationService: INavigationService
{

    private NavigationPage _navPage;

    public void Initialize(NavigationPage navPage)
    {
         _navPage = navPage;
    }

    public void NavigateTo(String pageKey)
    {
        // Get Page from pageKey
        _navPage.PushAsync(page);
    }
}

If you want to see how it is done in MVVMLight you can look here: http://www.mvvmlight.net/doc/nav1.cshtml

You can just use the ServiceLocator or other to get the navigation service as needed, whether its in native code or not.

OR

The other way around is that you Dependency Inject another Service type and load up another class from inside Forms. Then you just pass the action through to that, and it can perform the navigation while you are inside Forms.

I've use this tutorial for your purpose, I hope to help you