Huawei Xamarin SDK Setup

Follow these instructions if your app is distributed on the Huawei AppGallery.

Step 1. Requirements

Step 2. Setup the OneSignal Xamarin SDK

Follow the OneSignal Xamarin SDK setup guide. Firebase / Google setup not required for app builds released to the Huawei AppGallery. You will need to move the initialization code later in the guide.

Step 3. Huawei Setup

3.1 Preparing the Development Environment
See Huawei's Preparing the Development Environment.

Step 4. Using HMS Core SDK in Android App

5.1 Integrate HMS Core SDK and move OneSignal initialization to the OnCreate method.

  • Example package structure for agconnect-services.json, MainActivity.cs, and HmsLazyInputStream.cs

  • MainActivity.cs code snippet for setting HMS configuration using HmsLazyInputStream.cs
using Android.Content;
using Android.OS;
using Com.Huawei.Agconnect.Config;
using Com.OneSignal.Sample.Shared;
using System;

namespace Com.OneSignal.Sample.Droid
{
    [Activity(Label = "Com.OneSignal.Sample.Droid", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : Activity
    {
    
        protected override void AttachBaseContext(Context context)
        {
            base.AttachBaseContext(context);
         
            // Read the agconnect-services.json so that the config is set for Huawei to register for a pushToken
            AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(context);
            // Refer to Huawei docs or below on how to create your HmsLazyInputStream class
            config.OverlayWith(new HmsLazyInputStream(context));
        }

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

            // Init OneSignal through a Shared class, easier to init for both Android and iOS applications
            SharedPush.Initialize();
        }
    }
}
  • HmsLazyInputStream.cs code snippet for parsing agconnect-services.json
using Android.Content;
using Com.Huawei.Agconnect.Config;
using System;
using System.IO;

namespace Com.OneSignal.Sample.Droid
{
    public class HmsLazyInputStream : LazyInputStream
    {
        public HmsLazyInputStream(Context context) : base(context)
        {
        }

        public override Stream Get(Context context)
        {
            try
            {
               // Trying to read agconnect-services.json file
               return context.Assets.Open("agconnect-services.json");
            }
            catch (Exception e)
            {
               // Failed to read agconnect-services.json file
               return null;
            }
        }
    }
}

Notes/Resources

  • HMS error code reference to help debug logs and successfully integrate HMS Core SDK with the OneSignal SDK.

Keystore Signature