user8013509
user8013509

Reputation: 151

Xamarin, Firebase: " Default FirebaseApp is not initialized in this process" - though initializeApp() is called?

Im starting to go crazy over that line:

Java.Lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.TalkAboutTv.TalkAboutTv. Make sure to call FirebaseApp.initializeApp(Context) first.

I have no clue what is causing that. I AM calling FirebaseApp.IntApp(this) and my package name is matching ... Why is it not working?

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
}



protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView (Resource.Layout.Main);

    firebase = new FirebaseClient("2131099704");
    Firebase.FirebaseApp.InitializeApp(this);

    FirebaseDatabase.Instance.GetReference("chats").AddValueEventListener(this);

    fab = FindViewById<Button>(Resource.Id.fab);
    edtChat = FindViewById<EditText>(Resource.Id.input);
    lstChat = FindViewById<ListView>(Resource.Id.list_of_messages);


    fab.Click += delegate
    {

        PostMessage();
    };



    if (FirebaseAuth.Instance.CurrentUser == null)
        StartActivityForResult(new Android.Content.Intent(this, typeof(SignIn)), MyResultCode);
    else
    {
        Toast.MakeText(this, "Welcome " + FirebaseAuth.Instance.CurrentUser.Email, ToastLength.Short).Show();
        DisplayChatMessage();
    }
}

private async void PostMessage()
{
    var items = await firebase.Child("chats").PostAsync(new MessageContent(FirebaseAuth.Instance.CurrentUser.Email, edtChat.Text));
    edtChat.Text = "";
}

public void OnCancelled(DatabaseError error)
{

}

public void OnDataChange(DataSnapshot snapshot)
{
    DisplayChatMessage();
}

private async void DisplayChatMessage()
{
    lstMessage.Clear();
    var items = await firebase.Child("chats")
        .OnceAsync<MessageContent>();

    foreach (var item in items)
        lstMessage.Add(item.Object);
    ListViewAdapter adapter = new ListViewAdapter(this, lstMessage);
    lstChat.Adapter = adapter;
    }
}

}

Upvotes: 6

Views: 9903

Answers (6)

Franz Gilbert
Franz Gilbert

Reputation: 1

I had that issue today, you will need to avoid more than 1 application in your Firebase project, as a turnaround and be sure to choose the "GoogleServiceJson" for the generation of your google-service.json

Upvotes: 0

Ruby Kousinovali
Ruby Kousinovali

Reputation: 347

This error came to me when i changed the contents of the .json file pointing to a new firebase database.

I cleaned the project and build it again and it worked.

Upvotes: 0

Abiel
Abiel

Reputation: 1

if none of the above has worked for you. check the project settings. in 'AndroidManifest.xml' add or verify you have the following. inside application <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" /> <receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="${id app from firebase console }" /> </intent-filter> </receiver>

Upvotes: 0

Rob Lyndon
Rob Lyndon

Reputation: 12681

One thing you don't need to do any more is to call FirebaseApp.initializeApp(this) from your MainActivity.OnCreate() method. If you're doing this, you should probably remove that call, as it's unnecessary and may cause downstream complications.

I tried deleting the bin and obj folders, I made sure that my google-services.json had the correct GoogleServicesJson build type, and that the build type was being correctly picked up in my fsproj (or csproj if you're still using C#) file. Yet the error was still happening.

What I had done wrong was that I had a typo in my package name. If you have performed all of the steps above, check the package_name field in your google-services.json file, and make sure it corresponds exactly with your project's package name. Then clean and rebuild your project.

Upvotes: 5

stepheaw
stepheaw

Reputation: 1713

I was spinning up a new app and I did not have Internet Permissions enabled. This fixed my issue

Upvotes: 3

alberto basoli
alberto basoli

Reputation: 316

Try to initialize firebase within your Application class (OnCreate method). But first download the google-services.json file from the project console, and make sure it has the "GoogleServicesJson" value as Build Action.

Upvotes: 9

Related Questions