Xamarin (Microsoft) is a multi-system development platform for mobile services that many developers use. Many AppGallery Connect services now support Xamarin, including Remote Configuration.
Remote Config allows you to make changes to your app remotely by making use of variables that you can define in the AppGallery Console. Different values can be set for different audiences, locations or sections of users. This is a great way to personalise your application, carry out testing of new features or just make updates without having to deploy a new app version.
Install the Xamarin environment and project setup.
You'll need to first download and install Visual Studio 2019.
Open Visual Studio and select Mobile development with .NET to install the Xamarin environment.
Next make sure you have enabled the Auth Service in AppGallery Connect.
Open Visual Studio, click Create a new project in the start window, select Mobile App (Xamarin.Forms), and set the app name and other required information.
Right-click your project and choose Manage NuGet Packages.
Search for the Huawei.Agconnect.RemoteConfiguration package on the displayed page and install it.
Download the JSON service file from your AppGallery project and add it into the *Assets directory in your project.
Create a new class named HmsLazyInputStreams.cs, and implement the following code to read the JSON file.
using System; | |
using System.IO; | |
using Android.Content; | |
using Android.Util; | |
using Huawei.Agconnect.Config; | |
namespace AppLinking1 | |
{ | |
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; | |
} | |
} | |
} | |
} |
Create a ContentProvider class and add the following code for your file to be read once your app is launched. Ensure that the package name set in ContentProvider, and the one in your project and the one set in AppGallery Connect are the same as the app package name.
using System; | |
using Android.Content; | |
using Android.Database; | |
using Huawei.Agconnect.Config; | |
namespace XamarinHmsRemoteConfig | |
{ | |
[ContentProvider(new string[] { "com.huawei.cordova.remoteconfig.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(); | |
} | |
} | |
} |
Right-click your project and choose Properties. Click Android Manifest on the displayed page and set a package name
Set in app default parameter values
You can configure a default parameter value in either of the following ways:
Using an XML resource file. Add a default parameter value XML file to the res/xml directory of your project. Call the ApplyDefault method to read the file.
Or you can use a Map object. Set a default parameter value dynamically through coding.
IDictionary<string, Java.Lang.Object> ConfigVariables = new Dictionary<string, Java.Lang.Object>(); | |
ConfigVariables.Add("value1", "Default"); | |
AGConnectConfig.Instance.ApplyDefault(ConfigVariables); |
Fetch the updated parameter value
Call the fetch API to fetch the updated parameter value from Remote Configuration. The default update interval used by the fetch API is 12 hours and can be set as required.
AGConnectConfig.Instance.Fetch(fetchInterval).AddOnSuccessListener(new TaskListener(this)).AddOnFailureListener(new TaskListener(this)); |
Obtain all parameter values
For Xamarin.Android apps, the MergedAll API is called to obtain all parameter values including the in-app default parameter value and the on-cloud parameter value. For Android apps, the getMergedAll API is used
And that's all for integrating Remote Configuration into Xamarin.Android apps. As more and more AppGallery Connect services support Xamarin, I will keep you posted about further integration tutorials.
Top comments (0)