Huawei Xamarin SDK Setup

OneSignal Huawei Xamarin Setup Guide

1. Xamarin OneSignal SDK Setup

Follow the Xamarin SDK Setup instructions.

2. Huawei Setup

3. Create and Integrate Android Binding Libs

279 306

4. Using HMS Core SDK in Android App

  • 4.1 Integrate HMS Core SDK
  • Example package structure for agconnect-services.json, MainActivity.cs, and HmsLazyInputStream.cs
314
  • 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

  • This is an example Xamarin project integrating the binding libs for HMS Core SDK.

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

Keystore Signature