Send HMS and GMS Push Notifications Using Firebase Cloud Functions and Firestore

Başar Akşanlı
Huawei Developers
Published in
6 min readJan 17, 2022

Hi everyone,

In this article, we will send push notifications to both HMS and GMS devices using Firebase Cloud Functions and Firestore. Since the Huawei ecosystem does not fully support Google Mobile Services anymore, the process to develop this might look a little bit tricky. But you will learn how to implement this kind of system in this article.

1. Getting Started

First of all, we will configure our project for Cloud functions and Firestore. We are going to use firestore as our database since Cloud functions work with it perfectly. We will store our token data and the information in the firestore. And, Cloud functions will listen to the changes on a collection. If there is an object is newly created, push notifications will be triggered. Using Firebase Cloud functions, we can directly send push notifications to GMS devices using the internal library, however, we will need to use POST requests to get access token for HMS and to send push notifications to HMS devices.

To get started, we will need to configure our project for Firebase firestore, cloud functions, Firebase Push notifications, and HMS Push Kit.
You can find the information to configure your project in the links below;

To get started, we will need to configure our project for Firebase firestore, cloud functions, Firebase Push notifications, and HMS Push Kit.

You can find the information to configure your project in the links below;

1- HMS Push Kit

2- Firebase Firestore

3- Firebase Cloud Messaging

Firebase Cloud Functions does not require any action on client-side.

- G+H Solution

I highly recommend you to use the G+H(Google + Huawei) solution of Huawei in order to make the Push services work according to the core service of the device. You can refer to this article to get more information on G+H.

2. Configuring the structure of your Firestore according to your needs

Cloud functions have the ability to be triggered, once an entry has been created in the specified collection in the firestore. We are going to develop a cloud function, which will allow us to send a push notification, once the entry has been created. According to this logic, first, we need to create a collection through firestore. Let’s assume that we have a structure as represented below, as we would like to build a real-time messaging app:

Chat_Messages / -Chat Id- / Messages / -Message Id-

Since we are planning to listen to all the changes in messages in every chat, we built our structure as above. Chat_Messages and Messages represent collections and Chat Id and Message Id are variables depending on their Ids.

In addition to these steps, we will need to get token information of the user and store that information in the User object at firestore.

Please refer to HMS Push Kit, and Firebase Cloud Messaging to get more information about how to get push token for users.

3. Developing Cloud functions for Firebase Cloud Messaging

First we need to create a Cloud Function, by clicking Create Function on Google Cloud Platform, after we create our project there.

Create Function on Google Cloud Platform

Then we configure our project as below. You can choose the region according to your preferences.

As it is seen above, we set trigger type as Cloud Firestore, event type as create, and Document Path as Chat_Messages/{chat_id}/Messages/{message_id} since we have a structure like that at firestore. We are using these brackets “{}” since we would like to define them as variables. This helps our function to understand to listen to all messages in every chat.

After we save our configurations, we can start coding our function.

- Coding Function to send push notifications to GMS devices

Now, we can start coding inside of index.js file. First, we need to add two dependencies as below;

Afterwards, we initialize the app and start defining our function.

As you see above, we defined the onCreate function to listen to the collections in our structure. Then we get the necessary information from the snapshot.

In this context, we need to get the message owner id, message receiver id, and the content of the message. We are going to use this information to send a push message to the receiver.

By querying the receiver and the sender IDs in the user collection at firestore, we can directly get all information of those users. We will collect the full name of the sender user, and collect the push token of the receiver user since we stored all push tokens inside the User object.

After we get our user objects, we directly access our receiver user’s tokens by messageReceiverUser.data()[‘cloudTokens’]. We store more than one token here and the information that if those tokens are HMS or GMS.

In cloud function, we collect all tokens, distinguish all tokens if they are HMS or GMS, then according to this information, we start the process of sending the push notifications.

Using admin.messaging().sendMulticast(options) function, we directly send our push notifications to GMS devices. There are no further actions needed.

However, we need to use POST requests for HMS devices

4. Send Push Notifications to HMS devices

In order to send pushes to HMS devices, we need to use HTTP requests, and consequently, we need to add the relevant dependency.

Besides, in order to send push notifications to the application you created on Huawei Developer Console, you will need to collect access token every hour by making a POST request to Huawei Servers.

In order to do that, we create another cloud function, but this time scheduled one. Below you can find the complete function;

This function gets the access token from Huawei servers every 60 min. With this access token, you will be able to send push notifications by using POST requests. Every 60 min, we store our token in our database, and in the sendChatNotification function, we retrieve it.

To make this function work, you will need to get the client_id and client_secret of your project in the Huawei Developer console and change those variables in the function.

Now we can continue implementing push notifications for HMS devices in sendChatNotificationFunction.

In order to implement this, we will need to get our fresh Access token from our database. We will use that token in the next section.

You can directly send push notifications to HMS devices using the code above and the HMS access token. However, you need to change appId in the URL section of the request with your own appId specified in the project at Huawei Developer Console.

Results

  • GMS Device
  • HMS Device

Conclusion

You have learned how to send push notifications to both GMS and HMS devices using Cloud Functions and Firestore in this article. But be aware that post requests using Cloud Functions can be paid features. For any questions and problems, you can reach us through Huawei Developer Forum.

See you in my next article! 🙂

References

Firebase pricing page

Google Cloud Platform

HMS Push Token

Firebase Cloud Messaging

--

--