Intermediate: Huawei Multi Kits (Push and Location) Integration in Unity Game

Lokesh Suryan
5 min readMay 21, 2021

--

Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover integration of Huawei Kit in Unity Project using Official Plugin (Huawei HMS AGC Service). Here we will cover below kits.

  • Push kit
  • Location Kit

Push Kit Introduction

Huawei Push Kit is a messaging service provided for you to establish a messaging channel from the cloud to devices. By integrating Push Kit, you can send messages to your apps on user devices in real time. This helps you to maintain closer ties with users and increases user awareness and engagement with your apps. You can click here to watch the MOOC video about Push Kit.

Service use case

Location Kit Introduction

Location Kit combines the Global Navigation Satellite System (GNSS), Wi-Fi, and base station location functionalities into your app to build up global positioning capabilities, allowing you to provide flexible location-based services for global users. Currently, it provides three main capabilities: fused location, activity identification, and geofence. You can call one or more of these capabilities as needed.

  • Fused location: Provides a set of easy-to-use APIs for your app to quickly obtain the device location based on the GNSS, Wi-Fi, and base station location data.
  • Activity identification: Identifies user activity status through the acceleration sensor, cellular network information, and magnetometer, helping you adapt your app to user behaviour.
  • Geofence: Allows you to set an interested area through an API, so that your app can receive a notification when a specified action (such as leaving, entering, or staying in the area) occur.

Service use case

Fused Location

If your app needs to obtain the device location, you need to apply for the location permission for your app, call the requestLocationUpdates method of Location Kit, set request parameters in LocationRequest, and specify a location mode as needed. To stop obtaining location information, call the removeLocationUpdates method.

Geofence

You can call the createGeofenceList method to create a geofence based on the location that is of interest. Then, Location Kit can sense the distance between the current device location and the geofence. When the device enters the geofence, a notification will be sent to your app. In addition, Location Kit can detect the duration at which the device stays in the geofence, and send a notification to your app if the stay duration reaches your pre-set limit.

You can also create a geofence by dragging to select an area on the map and setting relevant parameters. For details, refer to Server Development.

Development Overview

You need to install Unity software and I assume that you have prior knowledge about the unity and C#.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.
  • A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK installation package.
  • Unity software installed.
  • Visual Studio/Code installed.
  • HMS Core (APK) 4.X or later.

Follows the steps.

1. Create Unity Project.

  • Open unity Hub.
  • Click NEW, select 3D, Project Name and Location.
  • Click CREATE, as follows:

2. Click Asset Store, search Huawei HMS AGC Service and click Import, as follows.

3. Once import is successful, verify directory in Assets > Huawei HMS Core App Services path, as follows.

4. Choose Edit > Project Settings > Player and edit the required options in Publishing Settings, as follows.

5. Generate a SHA-256 certificate fingerprint.

To generating SHA-256 certificate fingerprint use below command

6. Download agconnect-services.json and copy and paste to Assets > Plugins > Android, as follows.

7. Choose Project Settings > Player and update package name.

8. Open LauncherTemplate.gradle and add below lines.

9. Open “baseProjectTemplate.gradle” and add lines, as follows.

10. Open “mainTemplate.gradle” and add below lines.

11. Open AndroidManifest file and add below permissions

12. Create Scripts folder and create a class.

13. Follow the steps, as shown in image:

a. Assign GameManager script to Canvas.

b. Select Button and add onClick event

c. Assign button to button handler.

14. Onclick Button Handler you find your script GameManager (As per your script name) and attach method as per below screenshot.

15. To build apk and run in device, choose File > Build Settings > Build for apk or Build and Run for run on connected device.

Result

  1. Click on getToken Button token is generated as per below screenshots and send notification base on device token.

2. Click on GetLocation button you can see result (Latitude and logitude) as per below screenshots.

Tips and Tricks

  • Always use the latest version of the library.
  • Add agconnect-services.json file without fail.
  • Add SHA-256 fingerprint without fail.
  • Make sure dependencies added in build files.

Conclusion

We have learnt integration of Huawei Push service and Location Kit into Unity Game development. Push Kit provides notification through the Ag-consoles using push token.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

--

--

No responses yet