👨🏼‍💻 Intermediate | HarmonyOS Push Kit & Location Kit |Earthquake Notifier App Part 2

Ahmet Yunus Sevim
Huawei Developers
Published in
4 min readApr 27, 2022

Introduction

In this article we will continue to develop Earthquake Notifier app. In this part we will retrieve user location with HMS Location Kit and send notifications to the user with HMS Push Kit.

If you haven’t checked part1 you can check it from here:

Requirements

  • Dev Eco Studio IDE

What Will You Learn

How to:

  • Use HMS Location Kit on HarmonyOS
  • Use HMS Map Kit on HarmonyOS
  • Retrieve access token with Retrofit

Development

1- Project Hierarchy

Project Hierarchy

In part2, I felt the need to show the hierarchy of projects because of I added new classes and move some methods to new classes. I moved code related to the calling earthquake API to new class EarhquakeApiProvider. Also added new model classes needed for notifications.

2- Location Kit

First of all, we must implement the Location Kit. Let’s enter the following lines into the app level gradle file.

We will create helper class for handling location functionalities named LocationProvider.

We will create a method checks device location settings and inform user if locations are disabled.

For location kit we need to ask location permission from user in a dynamic way. First thing to do is add permission to the config.json file.

Now, we will create methods for checking location permission is granted and requesting permissions from user to LocationProvider class.

Location Kit has a feature that retrieves location in dedicated intervals. We will add to method for starting location requests and stopping.

As you see code above startLocationUpdate method uses a interval value. This value indicates requesting user location interval. Also both method uses LocationCallback object. We will implement and pass this callback object while calling this methods.

That’s all for LocationProvider class. Now we can gather user location with this class. As I said before I changed project structure little bit. Move earthquake related code to new class. I wont explain further about that part since this article isn’t about that. But if you want to look for that I will share project on references.

We want to change user marker on map according to the location update and check earthquakes distance with user location.

Let’s jump to MainAbilitySlice:

We will create LocationProvider instance and callback object. When location retrieved successfully we will save user location and move map camera to location. We use Circle object to mark user location and move it with setCenter method. Also we will check location settings and as location permission from user here. Finally we can call startLocationUpdate method.

3- Push Kit

Push kit must be enabled from AGC. You can see how to enable it from here.

Firstly we need to implement Push kit dependency into the app level gradle file and create service ability extends HmsMessageService. Also we need to update config.json file.

Push Kit requires access token and push token in order to send notifications. In order to retrieve access token we need to send post request. We will handle this with retrofit. We will update ApiClient and ApiInterface classes that we create on part1. Also to sending notifications we need to send post request. We will also use Retrofit for it. You can see base URLs below.

To retrieve access token you need to post request with 3 parameters. We will explain them later. Notification request needs 2 parameters. Auth parameter is access token that we retrieve above and notificationMessage body part of request that holds notification data.

Access token model class:

Now we will create helper class that handles notification related functionalities.

Now we need to get push token:

And we need to get access token. In order to get access token we will call retrofit service we create and pass parameters. First parameter grantType is constant value. To get clientId and clientSecret you can get them from AGC under App Information section.

AGC: client ID and client secret

Now we can send notifications.

Since notification body has complex structure we will create model class for it. Notification body structure:

It’s all set. You can send notifications with creating instance of NotificationSender and calling sendNotification method. Notifications will be sent when new earthquakes detected. To do that we need another service runs background and checks new earthquakes in intervals. This is another articles’ topic since it’d make this article too long to read.

Tips & Tricks

  1. Don’t forget to enable Push Kit on AppGalleryConnect.
  2. Add the appgallery-connect.json file to the: entry>src>main>resources>rawfile directory.
  3. You can get AppId for push token from appgallery-connect.json > client/app_id.

Conclusion

We have developed a EarthquakeNotifier app on HarmonyOS with you. As in this project, We added location kit and get user location continuously and create notification service sends notifications to user when needed. I will create another branch in Github so code won’t mixed with part1. You can find project on references.

Thank you for reading. See you in my other articles…;)

References

https://developer.harmonyos.com/en/docs/documentation/doc-guides/document-outline-0000001064589184

--

--