Xamarin.android Default FirebaseApp is not initial

2019-02-21 12:35发布

问题:

I face issue with get token from firebase (push notification)

Default FirebaseApp is not initialized in this process com.ready_apps.Nebka.Business. Make sure to call FirebaseApp.initializeApp(Context) first.

even I called FirebaseApp.InitializeApp(this); in many places

MyApplication (that extend Application) , in onCreate of Activity where I call FirebaseInstanceId.Instance?.Token;

回答1:

Edit: This bug has been fixed in Xamarin.Firebase version 57.1104.0-beta1.

This error seems to be present in the newer versions of Firebase for Xamarin. I am also experiencing this error as of today, using the latest stable version 42.1021.1. (The error is also present in the latest beta build).

I found that a bug report has been filed for the issue here.

As mentioned in the bug report, deleting both the /obj and /bin folders in your Android project, and/or cleaning the project in Visual Studio should fix the problem temporarily until you update any resource that would change the Resource.Designer.cs file.

Downgrading to an older version of Firebase and Google Play Services is also possible before a permanent solution is available. I did not experience this error on Firebase and Google Play Services version 32.961.0, for example.



回答2:

Just Clean the solution once and run the app again.

This bug is already reported to Xamarin. https://bugzilla.xamarin.com/show_bug.cgi?id=56108

This solution is provided in their comment thread, it might get fixed in the newer release of xamarin NuGet package.



回答3:

I didnt fix it but I find walkaround this issue in debug mode only

I called this method onCreate() in activit I need to request the token

FirebaseInstanceId.Instance?.Token

here is the method

private void ConfigureFireBase()
        {

#if DEBUG
            try
            {
            Task.Run(() =>
            {
                var instanceId = FirebaseInstanceId.Instance;
                instanceId?.DeleteInstanceId();
                //Log.Debug("TAG", "{0} {1}", instanceId?.Token?.ToString(), instanceId.GetToken(GetString(Resource.String.gcm_defaultSenderId), Firebase.Messaging.FirebaseMessaging.InstanceIdScope));

            });

             // For debug mode only - will accept the HTTPS certificate of Test/Dev server, as the HTTPS certificate is invalid /not trusted
            ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
            }catch (Exception e)
            {
                Log.Debug("TAG", e.Message);
            }
#endif
        }