1

I'm integrating the Huawei Kit (Maps, Analytics, Crash and Push) into my Xamarin Forms App, but there is strange behavior, the agconnect-services.json is read only the first time I run the app, after that if I use this code var appId = AGConnectServicesConfig.FromContext(this).GetString("client/app_id"); the value is null. To read the configuration file again I have to uninstall the app, even in release mode, this happens. I downgraded all the nuget from Huawei, I use the bindings from https://github.com/johnthiriet/Xamarin.Android.Huawei.Hms.Demo, even I created my own bindings but nothing seems to work. I am using the Content Provider and the HmsLazyInputStream from the documentation.

public class HmsLazyInputStream : LazyInputStream
    {
        public HmsLazyInputStream(Context context)
            : base(context)
        {
        }

        public override Stream Get(Context context)
        {
            try
            {
                return context.Assets.Open("agconnect-services.json");
            }
            catch (Exception e)
            {
                Log.Error("Hms", $"Failed to get input stream" + e.Message);
                return null;
            }
        }
    }
[ContentProvider(new string[] { "com.test.qa.XamarinCustomProvider" }, InitOrder = 99)]
    public class XamarinCustomProvider : ContentProvider
    {
        public override int Delete(Android.Net.Uri uri, string selection, string[] selectionArgs)
        {
            throw new NotImplementedException();
        }

        public override string GetType(Android.Net.Uri uri)
        {
            throw new NotImplementedException();
        }

        public override Android.Net.Uri Insert(Android.Net.Uri uri, ContentValues values)
        {
            throw new NotImplementedException();
        }

        public override bool OnCreate()
        {
            AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(Context);
            config.OverlayWith(new HmsLazyInputStream(Context));
            return false;
        }

        public override ICursor Query(Android.Net.Uri uri, string[] projection, string selection, string[] selectionArgs, string sortOrder)
        {
            throw new NotImplementedException();
        }

        public override int Update(Android.Net.Uri uri, ContentValues values, string selection, string[] selectionArgs)
        {
            throw new NotImplementedException();
        }
    }

Initialization in MainActivity.cs

if (useHMSService)
           {
               AGConnectCrash.Instance.SetUserId("Anonymous user");
               HiAnalyticsTools.EnableLog();
               var instance = HiAnalytics.GetInstance(this);
               instance.SetAnalyticsEnabled(true);
               instance.SetReportPolicies(new List<ReportPolicy> { ReportPolicy.OnAppLaunchPolicy });

               MapsInitializer.SetApiKey("APIKEY");
               HmsMessaging.GetInstance(this).AutoInitEnabled = true; //Init push notifications

               var appId = AGConnectServicesConfig.FromContext(this).GetString("client/app_id");
               if (!string.IsNullOrWhiteSpace(appId))
               {
                   AlertDialog.Builder alert = new AlertDialog.Builder(this);
                   alert.SetTitle("AppID");
                   alert.SetMessage(appId);

                   Dialog dialog = alert.Create();
                   dialog.Show();
               }

           }

1 Answers1

-1

Your problem is related to using un-official binding and package.

Here is the link to the official NuGet package and document for how to use it.

The example show how to use map kit package and binding, but others packages and binding are available on the sample project at Github site.

https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides-V1/integrating-map-kit-0000001135925291-V1

Here the sample binding project that you can try out.

https://github.com/HMS-Core/hms-xamarin-bindings

Zinna
  • 1,947
  • 2
  • 5
  • 20