Forum Xamarin Xamarin.Forms

How to open a page from a notification

I have a shared project that uses DependencyService to send a local Notification on android.
But from this notification I can only start new Android specific activities or services using Intent.

How can I make it show one of the pages i already have in the shared project?

Answers

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited June 2015

    you need to play with the navigation stack to show the page you need
    can't you call PushAsync as usual?

  • JanGoessensJanGoessens NLMember

    No, I can't call any of the Navigation methods. I think those can only be called from xamarin.forms pages. And seeing that I can only get to an android activity or service, I can't do that

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited June 2015

    sure you can
    (Xamarin.Forms.Application.Current.MainPage as NavigationPage).PushAsync(..)
    NavigationPage basically implements the navigation service so you can enumerate the page stack and play with the pages

  • JanGoessensJanGoessens NLMember

    When I try that line of code, I get the following error:
    System.NullReferenceException: Object reference not set to an instance of an object

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    you need to put that code in a place where you know the native host Activity is already created

  • AndrewMobileAndrewMobile USMember ✭✭✭✭

    what exactly is null from all that? is Application.Current?
    aslo, I assumed you have a NavigationPage as the root page (MainPage of your app)

  • adamkempadamkemp USInsider, Developer Group Leader mod

    I posted this in a duplicate post:

    MessagingService is a good way to post a global in-process notification that some other code can handle. You could handle the message in your App class in the PCL.

  • AndrewMobileAndrewMobile USMember ✭✭✭✭
    edited June 2015

    you need to make sure first the Android Activity is created when doing anything related to Xamarin Forms

  • I am getting the same Object Reference Not Set (...) on

    (Xamarin.Forms.Application.Current.MainPage as NavigationPage).PushAsync(..)

    It is because "MainPage as NavigationPage" evaluate as null.

    Any idea how to move from the Notification to a PCL page ?

  • adamkempadamkemp USInsider, Developer Group Leader mod

    PushAsync only works with a NavigationPage. If you want to be able to go back then you have to make the MainPage of your app a NavigationPage. If you don't need to be able to go back then you can just overwrite the MainPage with a new page.

Sign In or Register to comment.