question

CastielTR-4177 avatar image
0 Votes"
CastielTR-4177 asked AmyPeng1-MSFT edited

How to Retrieve Webview URL from Firebase Realtime Database in Xamarin.android (c#) ?

How to Retrieve Webview URL from FIrebase Realtime Database in Xamarin.android (c#) ? I can't find any command or any examples for C#, Full of people are use the java but no one use c# android so ı need to help.

This is correctly I need the resource but this solution is write by java, I need the same as but in Xamarin.android(c#).

watch



Link the video: https://www.youtube.com/watch?v=ypRdHO-w8-o&list=PLblLWXxsNTovcxFGQh3D0CnVjsUXBuY8q&index=3

dotnet-csharpdotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered LeonLu-MSFT edited

Hello,​

Welcome to our Microsoft Q&A platform!

First of all, you need to install following nuget package in your xamarin.android project.

Xamarin.Firebase.Database


Then do not forget to download your google-services.json file from firebase console, and set the build action to GoogleServicesJson



Then you can use following code to retrieve Webview URL from Firebase Realtime Database in Xamarin.android

using Firebase.Database;



 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            WebView webView1 = FindViewById<WebView>(Resource.Id.webView1);
            webView1.Settings.JavaScriptEnabled = true;
            webView1.SetWebChromeClient(new MyWebChromeClient());
            string firebaseURL = "https://fir-databasedemo-xxxxa72.firebaseio.com/";
            var mDatabase = FirebaseDatabase.GetInstance(firebaseURL).GetReference("fir-databasedemo-xxxxa72");
            DatabaseReference reference = mDatabase.Child("url");

            reference.AddValueEventListener(new MyValueChangeLisenter(webView1));
}

 internal class MyWebChromeClient : WebChromeClient
    {
    }

    internal class MyValueChangeLisenter : Java.Lang.Object, IValueEventListener
    {
        private WebView webView1;

        public MyValueChangeLisenter(WebView webView1)
        {
            this.webView1 = webView1;
        }

        public void OnCancelled(DatabaseError error)
        {
           // throw new NotImplementedException();
        }

        public void OnDataChange(DataSnapshot snapshot)
        {
            // throw new NotImplementedException();

            webView1.LoadUrl(snapshot.Value.ToString());
        }
    }


118481-image.png

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our [documentation][4] to enable e-mail notifications if you want to receive the related email notification for this thread. [2]: /answers/storage/attachments/117837-image.png [4]: https://docs.microsoft.com/en-us/answers/articles/67444/email-notifications.html



image.png (14.2 KiB)
image.png (126.0 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have a error, the Null expectection, I am already the firebase connect to my project. I dont know why, please check it

117914-1.png
117924-2.png
117942-3.png


0 Votes 0 ·
1.png (34.9 KiB)
2.png (36.4 KiB)
3.png (44.1 KiB)

Please add a breakpoint in this line. check the value of webview and snapshot.

0 Votes 0 ·
CastielTR-4177 avatar image
0 Votes"
CastielTR-4177 answered LeonLu-MSFT edited

I have a error, the Null expectection, I am already connect the firebase to my project. I dont know why, please check it

117914-1.png
118462-image.png
This is my last day results, today ı have no data.
117942-3.png



image.png (57.8 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please create a structure like my following screenshot in firebase realtime database.

118438-image.png


1 Vote 1 ·
image.png (45.6 KiB)

Thank you for every information !

0 Votes 0 ·