Forum Xamarin Xamarin.Forms

How to load an url from a push message into webview

Hi Everyone,

Sorry for disturb you, I am very very very novice with app development and Xamarin. I am doing an app tha basicly have a webview and in that webview ocurrs everything (always load the same url, in this case the home of my webpage). I added the message push functionality and It works fine (oneSignal), but I want to know how to pass the launch url from the message to the webiew, this is my app class

and this is my mainpage.xaml

and this is my MainActivity.cs

Please, If anybody can help me I would appreciate forever

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    If the webview exists on your MainPage, we could define a property in the main page like:

    string launchURL;
    public string LaunchURL
    {
        set
        {
            launchURL = value;
            webView.Source = launchURL;
        }
        get => launchURL;
    }
    

    Pass the URL to this property in the notification opened event:

    private void HandleNotificationOpened(OSNotificationOpenedResult result)
    {
        string launchURL = result.notification.payload.launchURL;
    
        ((MainPage)MainPage).LaunchURL = launchURL;
    }
    
  • dzapata13dzapata13 Member ✭✭

    Hi @LandLu ,

    Thank you for your help, I made the changes but I got this error

    What do you think I can do to resolve this error?

  • LandLuLandLu Member, Xamarin Team Xamurai

    What does this error message mean? Could you please translate it to English?
    Moreover, change the event to non-static as I posted above.

  • dzapata13dzapata13 Member ✭✭

    Hie @LandLu

    Thanks for your help and pacience, I made the change and the error is resolved, but the URL that receive in themessage push doesnt open in webview, always open my index page. I put a logg in the HandleNotificationOpened method and It loggs the rigth url that I include in the push message.

    What do you think that its missing?

    App.xaml.cs file

    Mainpage.xaml.cs

    Mainpage.xaml

    Regards!!!

  • LandLuLandLu Member, Xamarin Team Xamurai

    Is the setter triggered in the mainpage?
    I saw you registered the navigating and navigated event of webview. Did they lead the webview to another source?
    Try to hard code the URL to https://www.microsoft.com, will the webview respond to it?

  • dzapata13dzapata13 Member ✭✭

    Hi @LandLu

    Thanks again for your help,

    The setter is triggered, but the variable lose the value when the mainpage constructor is executed. This the logging I put in the app and the code

  • LandLuLandLu Member, Xamarin Team Xamurai

    Did you instantiate a new instance of MainPage somewhere else?
    I saw you assign the app's main page using:

    MainPage = new MainPage();
    

    But the constructor you posted above has a parameter. It means you must instantiate a new instance of MainPage.
    If you do need to create a new page, try to pass the URL to it directly with the parameter like:

    new MainPage(launchUrl)

  • dzapata13dzapata13 Member ✭✭

    Hi @LandLu

    Thanks again for your help,

    The parameter in the constructor is unused, It was my first intent for trying to pass the launchurl to webview. I removed the parameter. I attached my files in xamarin project if you want to check it. There is no another place where I set the url for the webview.

    Regards!!!!

  • LandLuLandLu Member, Xamarin Team Xamurai

    It seems you registered twice both in Andoird main acitivy and Forms app.
    The Android part will create a new instance when the notification is opened.
    Try to remove the Android side code. We only need the initialization in the App to trigger the opened event.

Sign In or Register to comment.