How to Expose HUAWEI Push Kit features in Xamarin.Forms

Muhammed Enes Durmus
Huawei Developers
Published in
4 min readApr 13, 2021

Hello developers,

In this article, we are going to expose some of Huawei Mobile Services (HMS) Xamarin.Android Push Kit features for Xamarin.Forms by Dependency Services. Before starting you can take a look at Xamarin.Forms DependencyService Introduction and HUAWEI Push Kit and Xamarin.Android article. This article shows how to integrate Push Kit Plugin, send notification and data message to your Xamarin.Android Application.

Configuring App Information in AppGallery Connect

Before you get started, you must register as a Huawei Developer then create an app in AppGallery afterwards configure the signing certificate fingerprint and finally enable the Push Services. For this, please refer to Configuring App Information in AppGallery Connect.

Installing the Huawei Push Kit NuGet Package

HUAWEI Push Kit is available as a NuGet package. To use it, open NuGet Package Manager and install Huawei.Hms.Push. For details, please refer to Installing the Huawei Push Kit NuGet Package.

Setting Package Information in Xamarin

Push Kit works only in signed apps. Open Xamarin.Android project’s properties Android Package Signing section then fill the areas for both Debug and Release. For details, please refer to Setting Package Information in Xamarin.

Integrating the HMS Core SDK

Add following cs files to your Xamarin.Android project and it’s done. For details, please refer to Integrating the HMS Core SDK.

Necessary Classes

HMS Push Kit has lots of features therefore we are not going to expose all of them. Instead of that we are going to expose only the necessary ones. Such as HmsInstanceId, HmsMessaging and HmsMessageService.

HmsInstanceId

This class provides methods for obtaining the AAID and tokens required for accessing Push Kit. We will expose GetToken, DeleteToken, GetAAID and DeleteAAID methods.

Firstly, lets start with IHMSInstanceId interface in Xamarin.Forms project.

IHMSInstanceId

GetToken method is restricted to use in main thread, so we need to use an event to get result of it. Also we will trigger this event at HmsMessageService.OnNewToken because tokens are valid for limited time and we need to update it, regularly.

Secondly, we will need a HMSInstanceId class in Xamarin.Android project.

HMSInstanceId class

Why we have this static Instance property in this class? As I mentioned before we need to trigger events at HmsMessageService and the easiest way to trigger these events is using static property.

HmsMessaging

This class provides methods for subscribing to topics and for enabling or disabling the function of receiving notification messages. We will expose Subscribe, Unsubscribe, TurnOnPush, TurnOffPush methods and AutoInitEnabled property.

IHMSMessaging interface in Xamarin.Forms project,

IHMSMessaging interface

HMSMessaging class in Xamarin.Android project,

HMSMessaging class

HmsMessageService

It is a basic service class of Push Kit for receiving downlink messages or updated tokens. We will just use OnNewToken, OnTokenError and OnMessageReceived virtual methods.

IHMSPushEvent interface in Xamarin.Forms project,

IHMSPushEvent interface

In this case, we do not need to add OnNewToken and OnTokenError event again.

HMSPushEvent class in Xamarin.Android project,

HMSPushEvent class

HmsMessagingService class in Xamarin.Android project,

HMSPushMessageService Class

So let’s start to call these HMS Push Kit features.

Push Notification

Now, we have a token therefore we can send a notification. Open your project in AppGallery Connect then Growing > Push Kit > Add notification. Fill the areas as you wish then click “Test effect” button and paste your token there.

And that’s our first notification in Xamarin.Forms. Now your app is capable to use HMS Push Kit APIs.

--

--