A Novice Journey of Integrating Huawei Site Kit into Ionic Application ( Cross Platform Integration )

Sanghati Mukherjee
Huawei Developers
Published in
5 min readOct 30, 2020

Introduction

HMS Site Kit allow us to search places and in return provides variety of information such as address, location, phone number etc. We can search for places either by proximity or a text string. Our app can provide users with convenient and secure access to diverse, place-related services, and therefore acquire more users.

HMS Site Kit Services

  1. Place Search
  2. Nearby Place Search
  3. Place Details
  4. Place Search Suggestion
  5. Widget Search

In this article we will focus on Place Search, Nearby Place Search, Place Search Suggestion and Widget Search provided by HMS Site Kit.

Ionic Framework

Ionic Framework is an open source UI toolkit for building performant, high-quality mobile and desktop apps using web technologies such as HTML, CSS, and JavaScript with integrations for popular frameworks like Angular and React.

Think of Ionic as the front-end UI framework that handles all of the look and feel and UI interactions your app needs in order to be compelling. Unlike a responsive framework, Ionic comes with very native-styled mobile UI elements and layouts that you’d get with a native SDK on Android or iOS but didn’t really exist before on the web.

Since Ionic is an HTML5 framework, it needs a native wrapper like Cordova or Capacitor in order to run as a native app.

Here we will use Ionic framework with Angular and Capacitor as native wrapper.

Demo

Prerequisite

  1. Must have a Huawei Developer Account.
  2. Must have a Huawei phone with HMS 4.0.0.300 or later.
  3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.
  4. Must install node in the system
  5. Must install Ionic in the system using below command:

npm install -g @ionic/cli

Things need to be done

1. Generating a Signing Certificate Fingerprint. For generating the SHA key, refer this article.

2. Create an app in the Huawei AppGallery connect and enable ML Kit in Manage Api section.

3. Provide the SHA Key in App Information Section.

4. Provide storage location.

5. Download the agconnect-services.json and store it somewhere on our computer.

6. Create a blank Ionic Project using below command:

ionic start Your_Application_Name blank — type=angular

7. Download the Cordova Site Kit Plugin. Run the following command in the root directory of your Ionic project to install it through npm.

npm install <CORDOVA_SITE_KIT_PLUGIN_PATH>

8. If you want full Ionic support with code completion etc., install @ionic-native/core in your project.

npm install @ionic-native/core — save-dev

9. Run the following command to copy the “ionic/dist/hms-ml” folder from library to “node_modules/@ionic-native” folder under your Ionic project.

cp node_modules/@hmscore/cordova-plugin-hms-ml/ionic/dist/hms-ml node_modules/@ionic-native/ -r

10. Run the following command to compile the project:

ionic build
npx cap init [appName] [appId]

Where appName is the name of your app, and appId is package_name in your agconnect-services.json file (ex: com.example.app).

11. Run the following command to add android platform to your project:

ionic capacitor add android

12. Make sure your project has a build.gradle file with a maven repository address and agconnect service dependencies as shown below:

13. Add the Signing certificate configuration to the build.gradle file in the app directory as show below:

14. Add plugin to the build.gradle file in the app directory as show below:

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

15. Add site kit service implementation into to dependencies section of build.gradle file in the app directory as show below:

dependencies {implementation 'com.huawei.hms:site:5.0.0.300'}

16. Add agconnect-services.json and signing certificate jks file to the app directory in your Android project as show below:

17. To update dependencies, and copy any web assets to your project, run the following code:

npx capacitor sync

Let’s Code

Avoid No Provider Error

To avoid No Provider Error, we need to add FileChooser, Android Permission and HMS ML kit in app.module.ts providers section and also make sure to import the same as shown below:

Import

Import the following code in your home.page.ts :

import { Component, IterableDiffers } from ‘@angular/core’;
import { HmsSite } from ‘@ionic-native/hms-site/ngx’;

Initializing the service

First we need to initialize the service. In order to that we need to call the initializeService method to initialize the HMS Site Kit service using our project API key. The API key can be found in our agconnect-services.json file.

Place Search

Basically it helps us to extract information of a particular place like tourist attraction, enterprises and school by specifying keywords, coordinates and other information.

Place Search Suggestion

As the name suggest, it will return a list of suggestion of the place we search.

Nearby Place Search

As the name suggest, it will return a list of nearby places based on the current location of a user. When the user selects a place, the app obtains the place ID and searches for details about the place.

Widget Search

As the name suggest it is a widget that has a built-in place search suggestion feature. When a user enters a keyword in the search box, the widget displays a list of suggested places to the user.

Run application

Now we can run the application using the following command on visual studio terminal:

ionic capacitor run android

Conclusion

We learn how to integrate HMS Site Kit in Ionic Project. Using HMS Site kit we can create application such as tourism application, restaurant based application and so on. I hope this article will be very useful to you in order to create wonderful application using Ionic.

Feel free to comment, share and like the article. Also you can follow me to get awesome article like this every week.

For more reference

1. https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/introduction-0000001050260022

2. https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/initializeservice-0000001050260344

--

--