Sending Push Notifications over AGC Cloud Functions

Enes Kocaman
Huawei Developers
Published in
4 min readMay 28, 2020

Huawei Cloud Functions is a serverless execution environment that means no need to provision any infrastructure for writing simple, single purpose functions. Working with Cloud Functions frees you from server operations and management, load-balancing, scaling and etc.

In short cloud functions are small block of code that’s executed on the cloud. Allows you to split your backend architecture into small functions that do one thing but really well.

Currently Huawei Cloud Functions supports only Node.js 10.15.2 runtime environment and programming language nodejs V10 or later. Java and Python languages will be supported later.

There are some restrictions about size of a function deployment package as well

50 MB (compressed and can be directly uploaded)

100 MB (decompressed)

20 levels of directories

30,000 files

After these small definitions let’s make an real and small development example!

We ‘re going to send push notification to target mobile device over Cloud Functions using with Nodejs.

Pre-requirements

  • You will need to Client Id and Client Secret parameters to send request to Push API, you can get your App Id and App secret like i put an example ss at below.
App Id and App Secret

Let’s make a post request to obtain our App-Level access token. I used the “request” library to send http request, however you can use any other libraries. Due to the i have obtained app level access token before, i used the refresh token to obtain access-token. For details please refer to https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/38054564#h2-1580973380498

Now we have obtained App-Level access token. Let’s have a look at what we need to send push notification over push api;

HMS — Push Kit API

For more details, please refer to : https://developer.huawei.com/consumer/en/doc/development/HMS-References/push-sendapi

HEADER : Bearer [App-Level Access Token] (Mandatory)

BODY : Message (Mandatory) & Validate_only (Optional)

I have created regarding options which includes message body(with device token) and app-level access token(i put that value as a parameter of pushToken function)

We will need “push token” to sent push notifications to target devices.

Obtaining Push Token of Target Device

Please follow the article to obtain push token,

Now, if you have Push Token we can put it on token area on message payload like below.

{
“validate_only”: false,
“message”: {
“notification”: {
“title”: “Push from Server”,
“body”: “Something..!”
},
“android”: {
“notification”: {
“title”: “Push from Server”,
“body”: “Sending push notification using AGC Cloud Functions”,
“click_action”: {
“type”: 1,
“intent”: “#Intent;compo=com.rvr/.Activity;S.W=U;end”
}
}
},
“token”: [
“PUT YOUR PUSH TOKEN HERE”,

]
}
}

You can use sample message body directly which provided for developers by regarding link : https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/push-rest-v3-sample

Finally call the myHandler.js and don’t forget to export the handler function;

After all, we need to create a function on AGC Cloud Functions console.

Please follow this document to enable AGC Cloud Functions and create function on App Gallery Console:

https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-cloudfunction-getstarted#h1-1578318774130

In this step don’t forget to select Code Entry Type as .zip file.

In this step don’t forget to select Code Entry Type as .zip file.

Packaging the project

Then create the zip file of the project as you can see how to do it at above and upload it by selecting file in Function Code section.

Now, we can test our work!

You can type anything on Event section its doesn’t matter but it shouldn’t be empty.

Execute the request

After clicked to Test, you will be able to see retrieved notification on your phone.

Retrieved Push Notification

Conclusion

As we can see in this example, we moved simple backend side of push notification of our application to AGC Cloud Functions and run the project on Nodejs environment, in this way we don’t need to work and spend time on server side management operations, implementations and load-balancing etc.

We just need to develop backend side then deploy it on the Cloud Functions run time environment.

References

--

--